python - Return the column name(s) for a specific value in a pandas dataframe -


where have found option in other languages such r or sql not quite sure how go in pandas.

so have file 1262 columns , 1 row , need column headers return every time specific value appears.

say example test dataframe:

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 need locate column name e.g. value = 38.15. best way of doing so?

thanks

seeing have single row can call iloc[0] on result , use mask columns:

in [47]: df.columns[(df == 38.15).iloc[0]]  out[47]: index(['col7'], dtype='object') 

breaking down 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 use idxmax param axis=1:

in [52]: (df == 38.15).idxmax(axis=1)[0]  out[52]: 'col7' 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -