简体   繁体   中英

Get index of column that contains specific values Pandas

I want to find index of column that contains specific value. I have big dataset (more than 2500 columns) and value can be in any of the columns.

So, I do not know name of the column where is that value. Dataset is big, more than 2500 columns, and value can be in any of 2500 columns.

I need column index, not row index

This is only an example dataset:

import pandas as pd

#THIS IS THE EXAMPLE ONLY, DATASET IS VERY BIG 
df = pd.DataFrame({'one':[1,2,3,4,5],
                   'two':[45,4,3,2,4],
                   'three':[4,2,121,111,5]})
print(df)

i = df[df==111].index
print(i)
#RESULT SHOULD BE INDEX 2

Try stack and idxmax

n = (df.stack() == 111).idxmax()[1]
df.columns.get_loc(n)

Out[572]: 2

This should work:

df.eq(111).idxmax().max()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM