pandas 如何检查数据框中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47348157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:47:01  来源:igfitidea点击:

how to check if a value exists in a dataframe

pythonpandasdataframedata-analysis

提问by pyd

hi I am trying to get the column name of a dataframe which contains a specific word,

嗨,我正在尝试获取包含特定单词的数据框的列名,

eg: i have a dataframe,

例如:我有一个数据框,

NA              good    employee
Not available   best    employer
not required    well    manager
not eligible    super   reportee

my_word=["well"]

how to check if "well" exists in a df and the column name which has "well"

如何检查df中是否存在“well”以及具有“well”的列名

thanks in Advance!

提前致谢!

回答by jezrael

Use DataFrame.isinfor check all columns and DataFrame.anyfor check at least one Trueper row:

使用DataFrame.isin用于检查所有的列和DataFrame.any用于检查至少一个True每行:

m = df.isin(my_word).any()
print (m)
0    False
1     True
2    False
dtype: bool

And then get columns names by filtering:

然后通过过滤获取列名:

cols = m.index[m].tolist()
print(cols)
[1]

Data:

数据:

print (df)
               0      1         2
0            NaN   good  employee
1  Not available   best  employer
2   not required   well   manager
3   not eligible  super  reportee

Detail:

细节:

print (df.isin(my_word))
       0      1      2
0  False  False  False
1  False  False  False
2  False   True  False
3  False  False  False

print (df.isin(my_word).any())
0    False
1     True
2    False
dtype: bool

EDIT After converting get nested lists, so flatteningis necessary:

编辑转换后得到嵌套lists,所以扁平化是必要的:

my_word=["well","manager"]

m = df.isin(my_word).any()
print (m)
0    False
1     True
2     True
dtype: bool

nested = df.loc[:,m].values.tolist()
flat_list = [item for sublist in nested for item in sublist]
print (flat_list)
['good', 'employee', 'best', 'employer', 'well', 'manager', 'super', 'reportee']