-1

enter image description here

Hi, I am using .columns attribute in pandas and I am getting INDEX at the beginning, can someone please let me know that why INDEX is mentioned at the beginning.

Mrinal Roy
  • 969
  • 7
  • 12
  • 1
    Because `.columns` returns a `pandas.core.indexes.base.Index` object, which happens to have `Index` in its string representation – DeepSpace Jul 17 '20 at 15:12

1 Answers1

0

Answered before Index objects in pandas--why pd.columns returns index rather than list, official documentation .index.

Immutable ndarray implementing an ordered, sliceable set. The basic object storing axis labels for all pandas objects

If you are just expecting values then you've to go a little bit more. As .columns returns an Index , .columns.values returns an array and a helper function .tolist returns a list of column names.

car_sales.columns.values.tolist()

You can use this car_sales.columns.tolist() too but won't perform good in large dataframes.

Mrinal Roy
  • 969
  • 7
  • 12