Trying to get the column indexes with the column names in pandas

Viewed 379

I'm doing a project with an embedded jupyter notebook and when I pull up df.info() it has no header and no index numbers. I tried to do df.info(verbose=True) and that still didn't do it. On my desktop when i do df.info() it has column headers that say: #, column, non-null count, and dtype. I am just trying to get a list of every column name with its corresponding index value. Is there a better way to do this?

I want to see it like

0 Column1Name 1 Column2Name ...etc.

3 Answers
i = 0
for col in df.columns:
    print(i, col)
    i += 1

something like

indexed_columns = [(index, col) for index,col in enumerate(df.columns)]
print(indexed_columns)

It seems you are working in two different environments and that you are able to access the data frame locally but not in the other environment.

To test if that's the case, create a dummy data frame and test it as follows in your non-desktop environment:

>>> import pandas as pd
>>> pd.DataFrame({'X': [1,2,9,2,3,1,2,4],
                   'Y': [6,7,8,9,0,4,6,7]})
>>> df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   X       8 non-null      int64
 1   Y       8 non-null      int64
dtypes: int64(2)
memory usage: 256.0 bytes

If you're able to see this dummy data frame, you need to look at how the data frame you are expecting is created in your secondary environment.

Related