简体   繁体   English

获取包含特定值 Pandas 的列的索引

[英]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尝试stackidxmax

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.

相关问题 获取在熊猫中包含特定值的列名 - get column name that contains a specific value in pandas pandas 使用 UTC 索引获取列值 - pandas get column values using UTC index 如果熊猫数据框中包含特定的子字符串,则替换它的列值 - Replacing column values in a pandas dataframe based if it contains a specific substring 如何获取包含与索引对应的特定值的列列表作为 pandas dataframe 中的新列? - How to get list of columns containing specific values corresponding to a index as a new column in pandas dataframe? 熊猫:如何获取包含值列表的列的唯一值? - Pandas: how to get the unique values of a column that contains a list of values? 大熊猫重命名:仅更改特定列的索引值 - pandas rename: change values of index for a specific column only Python 3:获取与pandas dataframe某列中的值关联的索引名称 - Python 3: Get index names associated with the values in a certain column of pandas dataframe 获取 pandas df 中连续值为零的列的索引 - Get index of column where consecutive values are zero in pandas df 使用 str.contains 创建新列 Pandas df 给出:值的长度与索引的长度不匹配 - create new column Pandas df with str.contains gives: Length of values does not match length of index 通过特定列值选择熊猫数据框中的索引 - Get indices in pandas dataframe selecting by specific column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM