Reputation: 1987
I want the column index to be displayed along with the column names. Looking at the docs for df.info()
, using verbose=True
should help but it isn't.
import pandas as pd
records = {'a':['abc','def','ghi'], 'b':[22,41,13], 'c':[133,3123,552]}
df = pd.DataFrame(records)
df.info()
Data columns (total 3 columns):
a 3 non-null object
b 3 non-null int64
c 3 non-null int64
df.info(verbose=True)
Data columns (total 3 columns):
a 3 non-null object
b 3 non-null int64
c 3 non-null int64
I've written a function to do the same.
def get_features_with_indices(df):
for i in range(len(df.columns)):
print(i,df.columns[i])
get_features_with_indices(df)
0 a
1 b
2 c
But I'm looking for a better way. Is there a built-in pandas functionality to get this so it can be cleaner?
Upvotes: 1
Views: 34
Reputation: 18416
You can create frame out of column names:
df.columns.to_frame(index=False, name='cols')
OUTPUT:
cols
0 a
1 b
2 c
Upvotes: 2