How to list the index and columns names together for a given dataframe?

Viewed 183

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
1 Answers

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'}
Related