pandas 查看 DataFrame 中是否存在值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34573999/
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
See if a value exists in a DataFrame
提问by user3374113
In Python to check if a value is in a list you can simply do the following:
在 Python 中检查值是否在列表中,您可以简单地执行以下操作:
>>>9 in [1,2,3,6,9]
True
I would like to do the same for a Pandas DataFrame but unfortunately Pandas does not recognise that notation:
我想对 Pandas DataFrame 做同样的事情,但不幸的是 Pandas 无法识别该符号:
>>>import pandas as pd
>>>df = pd.DataFrame([[1,2,3,4],[5,6,7,8]],columns=["a","b","c","d"])
a b c d
0 1 2 3 4
1 5 6 7 8
>>>7 in df
False
How would I achieve this using Pandas DataFrame without iterating through each column/row or anything complicated?
我将如何使用 Pandas DataFrame 实现这一点而不遍历每一列/行或任何复杂的东西?
回答by Ezer K
Basically you have to check the matrix without the schema, so:
基本上你必须在没有模式的情况下检查矩阵,所以:
7 in df.values
x in df
checks if x
is in the columns:
x in df
检查是否x
在列中:
for x in df:
print x,
out: a b c d