检查 pandas 中任何列的任何行中是否存在一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19630328/
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
Check if one value exists in any rows of any columns in pandas?
提问by Blaszard
Is there any function to check if a value exists in any rows of any columns in pandas, such as
是否有任何函数可以检查pandas中任何列的任何行中是否存在值,例如
columnA columnB columnC
"john" 3 True
"mike" 1 False
"bob" 0 False
on the dataframe above, I want to know if there are any values named "mike"in any elements of the whole dataframe, and if it exists, I'd like to get True- otherwise get False.
在上面的数据框上,我想知道"mike"整个数据框的任何元素中是否有任何命名的值,如果存在,我想获取True- 否则获取False.
Thanks.
谢谢。
回答by Roman Pekar
Something like this:
像这样的东西:
df.apply(lambda x: 'mike' in x.values, axis=1).any()
or
或者
df.applymap(lambda x: x == 'mike').any().any()

