问题描述
我想基于整个熊猫的特定列中包含的特定值,从整个数据库中获取列名(假设数据库包含100多个行,其中50列以上).
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.
在Bkmm3(来自印度的成员)的帮助下,我在数字术语上取得了成功,但在字母术语上却失败了.我尝试过的方式是这样的:
with the help of Bkmm3 (member from india) I've succeeded on numerical term but failed on alphabetic term. the way I've tried is this:
df = pd.DataFrame({'A':['APPLE','BALL','CAT'],
'B':['ACTION','BATMAN','CATCHUP'],
'C':['ADVERTISE','BEAST','CARTOON']})
response = input("input")
for i in df.columns: if(len(df.query(i + '==' + str(response))) > 0):
print(i)`
然后输出出现错误:
Traceback (most recent call last): NameError: name 'APPLE' is not defined
非常感谢你们的帮助,谢谢. .
Any Help from You Guys will be very Appreciated, Thank You . . .
推荐答案
isin
/eq
适用于DataFrames,您可以将其100%向量化:
isin
/eq
works for DataFrames, and you can 100% vectorize this:
df.columns[df.isin(['APPLE']).any()] # df.isin([response])
或者,
df.columns[df.eq(response).any()]
Index(['A'], dtype='object')
这是DataFrame.eval
和np.logical_or
的回旋方式(要在列上循环):
And here's the roundabout way with DataFrame.eval
and np.logical_or
(were you to loop on columns):
df.columns[
np.logical_or.reduce(
[df.eval(f"{repr(response)} in {i}") for i in df]
)]
Index(['A'], dtype='object')
这篇关于获取列名,该列名在python pandas的任何行中都包含特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!