[英]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.
我有大数据集(超过 2500 列),值可以在任何列中。
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.
数据集很大,超过 2500 列,值可以在 2500 列中的任何一个中。
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
尝试
stack
和idxmax
n = (df.stack() == 111).idxmax()[1]
df.columns.get_loc(n)
Out[572]: 2
This should work:这应该有效:
df.eq(111).idxmax().max()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.