1

I have dataframe. I tried simple a function to convert column names to list. Surprisingly I am getting tuples. Why?

my code:

big_list = [[nan, 21.0, 3.2, 12.0, 3.24],
 [42.0, 23.799999999999997, 6.0, 13.599999999999998, 5.24],
 [112.0, 32.199999999999996, 14.400000000000002, 18.4, 11.24],
 [189.0, 46.2, 28.400000000000002, 26.400000000000002, 21.240000000000002]]
    df= pd.DataFrame(np.array(big_list),index=range(0,4,1),columns=[sns])


df = 

          ig      abc      def    igh    klm
    0      NaN    21.0     3.2   12.0    3.24
    1     42.0    23.8     6.0   13.6    5.24
    2    112.0    32.2    14.4   18.4   11.24
    3    189.0    46.2    28.4   26.4   21.24

print(list(df))

present output:

   [('ig',), ('abc',), ('def',), ('igh',), ('klm',)]

Expected outptu:

  ['ig','abc','def','igh','klm']
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • I would take a look at the documentation for `pandas`, I don't think that's the preferred way to get column titles, maybe `df.columns.values.tolist()`? (been forever since I used pandas so might be wrong) – M Z Aug 03 '20 at 16:31
  • How was your dataframe created? Would you like to post a [mre]? – khelwood Aug 03 '20 at 16:32
  • 1
    You have a broken MultiIndex. Do `df.columns = df.columns.get_level_values(0)` but really you should fix whatever made that broken columns Index. Probably somewhere you specified a column as a tuple with a trailing comma i.e. `('ig',)` which is not good for pandas. – ALollz Aug 03 '20 at 16:35
  • This might help https://stackoverflow.com/questions/19482970/get-list-from-pandas-dataframe-column-headers – Ishwarmani Aug 03 '20 at 16:36
  • @ALollz You are spot on. I edited my question. You can look at at column names in df. `[sns]`. This should have been in simple `sns`. This solved the problem. Thanks – Mainland Aug 03 '20 at 17:31

1 Answers1

1

The following code should do it:

df = pd.DataFrame([[np.nan, 21.0, 3.2, 12.0, 3.24]], columns=['ig','abc','def','igh','klm'])
print(list(df.columns))

It gives the following output:

['ig', 'abc', 'def', 'igh', 'klm']

If your output is different, then the dataframe might be wrongly constructed

Nles
  • 161
  • 4