简体   繁体   English

获取在熊猫中包含特定值的列名

[英]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.

相关问题 pandas获取包含值的列的名称 - pandas get the name of the column that contains a value 获取列名,其中包含python pandas中任何行的特定值 - Get column name which contains a specific value at any rows in python pandas 在 Pandas 中查找包含另一列行中特定值的列名 - Find column name in Pandas that contains a specific value in the row from another column Pandas:按名称和年份从特定行和列中获取值 - Pandas: get value from specific row and column by name and year 获取 Python Pandas 中每一行中特定值的列名 - Get Column Name for specific value in each row in Python Pandas 获取包含特定值 Pandas 的列的索引 - Get index of column that contains specific values Pandas Pandas 获取包含另一列值周围括号的列的列标题名称 - Pandas get column header name for column which contains bracket around value of another column 查找名称包含固定列中特定值的列 - Find column whose name contains a specific value that is in a fixed column pandas:在列包含特定值的行之后插入一行 - pandas: insert a row after a row where the column contains a specific value 如何在整个Pandas数据帧中搜索字符串,并获取包含该字符串的列的名称? - How to search entire Pandas dataframe for a string and get the name of the column that contains it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM