[英]get column name that contains a specific value in pandas
I want to get column name from the whole database (assume the database contains more than 100 rows with more than 50 column) based on specific value that contain in a specific column in pandas. 我想从整个数据库中获取列名(假设数据库包含超过100行,其中50列以上)基于熊猫中特定列中包含的特定值。
Here is my code: 这是我的代码:
import pandas as pd
df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9]})
pos = 2
response = raw_input("input")
placeholder = (df == response).idxmax(axis=1)[0]
print df
print (placeholder)
Tried a lot . 试了很多。 .
。 .
。
Example: when the user will input 2; 例:用户何时输入2; it will show answer: A if the input is 4;
它将显示答案:如果输入为4,则为A;否则为0。 feedback will be B and if 7 then reply will be C
反馈将为B,如果为7,则回复为C
tried iloc but I've seen row have to be noticed there. 尝试过iloc,但我已经看到必须在那儿注意行。
Please Help Dear Guys . 请各位朋友帮助。 .
。 .
。 .
。 .
。 Thanks .
谢谢 。 .
。 .
。 :)
:)
First of all you should treat the input as integer. 首先,您应该将输入视为整数。 So instead of
raw_input
, use input
: 因此,请使用
input
代替raw_input
:
response = input("input")
After that you can use any
: 之后,您可以使用
any
:
df[df==YOUR_VALUE].any()
This will return a boolean Series
with columns names and whether they contain the value you are looking for. 这将返回带有列名称以及它们是否包含您要查找的值的布尔
Series
。
In your example: 在您的示例中:
df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9]})
response = input("input")
placeholder = df[df==response].any()
for input 4 the output will be: 对于输入4,输出将是:
A False
B True
C False
dtype: bool
Try this 尝试这个
for i in df.columns:
newDf = df.loc[lambda df: df[i] == response]
if(not newDf.empty):
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.