0

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.

2
  • 4
    I think you're confusing index, that is a pandas index object, your columns are indeed strings and a list if you call more than one. What are you trying to do? Commented Mar 18, 2021 at 14:54
  • 4
    df.columns is always of type pd.Index no matter if you assign them as list. you can use them as lists where you want to. Commented Mar 18, 2021 at 15:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this is definitely not what the OP wants

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.