Return column names for a specific value in the pandas framework

where I found this option in other languages ​​such as R or SQL, but I'm not quite sure how to do this in Pandas.

So, I have a file with 1262 columns and 1 row, and for the column headings I need to return every time a certain value appears.

Say, for example, this test frame:

Date col1 col2 col3 col4 col5 col6 col7 01/01/2016 00:00 37.04 36.57 35.77 37.56 36.79 35.90 38.15 

And I need to find the column name, for example. where value = 38.15. What is the best way to do this?

thanks

+6
source share
3 answers

When you see that you have only one row, you can call iloc[0] as a result and use this to mask the columns:

 In [47]: df.columns[(df == 38.15).iloc[0]] Out[47]: Index(['col7'], dtype='object') 

Violation of the above:

 In [48]: df == 38.15 Out[48]: Date col1 col2 col3 col4 col5 col6 col7 01/01/2016 False False False False False False False True In [49]: (df == 38.15).iloc[0] Out[49]: Date False col1 False col2 False col3 False col4 False col5 False col6 False col7 True Name: 01/01/2016, dtype: bool 

You can also use idxmax with parameter axis=1 :

 In [52]: (df == 38.15).idxmax(axis=1)[0] Out[52]: 'col7' 
+7
source

You can use data frame splitting and then get the column names:

 df.ix[:,df.loc[0] == 38.15].columns 

exit:

 Index([u'col7'], dtype='object') 
+2
source

just to throw something a little in the ring:

 row = df.iloc[0] row.reset_index().set_index(0).loc[38.15] 
0
source

All Articles