Alex S.
Alex S.

Reputation: 1224

pandas DataFrame remove Index from columns

I have a dataFrame, such that when I execute:

df.columns

I get

Index(['a', 'b', 'c'])

I need to remove Index to have columns as list of strings, and was trying:

df.columns = df.columns.tolist()

but this doesn't remove Index.

Upvotes: 0

Views: 680

Answers (1)

Nike
Nike

Reputation: 1307

tolist() should be able to convert the Index object to a list:

df1 = df.columns.tolist()
print(df1)

or use values to convert it to an array:

df1 = df.columns.values

The columns attribute of a pandas dataframe returns an Index object, you cannot assign the list back to the df.columns (as in your original code df.columns = df.columns.tolist()), but you can assign the list to another variable.

Upvotes: 1

Related Questions