2

How to list the column names along with their index from a data frame in python ?

the below code gives only the index numbers of the column names. But i need to learn on how to list the index numbers along with the column names for a large dataset with multiple columns names.

enter code here:

columnnames=['a','b','c']
df.columns.get_loc(col) for col in  column_names
4
  • Use this: [df.columns.get_loc(c) for c in df.columns] Commented Nov 24, 2020 at 5:30
  • I did the same , but i could get only the index .I am looking for something that shows column names along with their index Commented Nov 24, 2020 at 5:34
  • pandas.pydata.org/pandas-docs/stable/reference/api/… Try Loc Commented Nov 24, 2020 at 5:40
  • @SaiSrija Check my answer. Commented Nov 24, 2020 at 5:42

1 Answer 1

2

You can create a map like this:

In [3359]: col_map = {df.columns.get_loc(c):c  for c in df.columns}

In [3360]: col_map
Out[3360]: {0: 'id', 1: 'count', 2: 'colors'}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.