I am trying to determine whether there is an entry in a Pandas column that has a particular value. I tried to do this with if x in df['id']. I thought this was working, except when I fed it a value that I knew was not in the column 43 in df['id'] it still returned True. When I subset to a data frame only containing entries matching the missing id df[df['id'] == 43] there are, obviously, no entries in it. How to I determine if a column in a Pandas data frame contains a particular value and why doesn't my current method work? (FYI, I have the same problem when I use the implementation in this answer to a similar question).


当前回答

我做了一些简单的测试:

In [10]: x = pd.Series(range(1000000))

In [13]: timeit 999999 in x.values
567 µs ± 25.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [24]: timeit 9 in x.values
666 µs ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [16]: timeit (x == 999999).any()
6.86 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [21]: timeit x.eq(999999).any()
7.03 ms ± 33.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [22]: timeit x.eq(9).any()
7.04 ms ± 60 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [15]: timeit x.isin([999999]).any()
9.54 ms ± 291 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [17]: timeit 999999 in set(x)
79.8 ms ± 1.98 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

有趣的是,不管你是查找9还是999999,似乎使用in语法所花费的时间是相同的(一定是使用了某种向量化计算)

In [24]: timeit 9 in x.values
666 µs ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [25]: timeit 9999 in x.values
647 µs ± 5.21 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [26]: timeit 999999 in x.values
642 µs ± 2.11 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [27]: timeit 99199 in x.values
644 µs ± 5.31 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [28]: timeit 1 in x.values
667 µs ± 20.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

似乎使用x.values是最快的,但也许在pandas中有更优雅的方法?

其他回答

found = df[df['Column'].str.contains('Text_to_search')]
print(found.count())

find .count()将包含匹配数

如果它是0,那么意味着字符串没有在列中找到。

或者用级数。tolist或Series.any:

>>> s = pd.Series(list('abc'))
>>> s
0    a
1    b
2    c
dtype: object
>>> 'a' in s.tolist()
True
>>> (s=='a').any()
True

系列。tolist做了一个关于一个系列的列表,而另一个我只是从一个常规系列中获得一个布尔系列,然后检查是否有任何真布尔系列。

使用query()查找符合条件的行,并获得形状为[0]的行数。如果存在至少一个条目,则此语句为True:

df.query('id == 123').shape[0] > 0

我有一个CSV文件要读取:

df = pd.read_csv('50_states.csv')

在尝试之后:

if value in df.column:
    print(True)

即使值在列中,它也不会输出true;

我试着:

for values in df.column:
    if value == values:
        print(True)
        #Or do something
    else:
        print(False)

这工作。希望这能有所帮助!

假设你的数据框架是这样的:

现在你要检查文件名“80900026941984”是否存在于数据帧中。

你可以简单地写:

if sum(df["filename"].astype("str").str.contains("80900026941984")) > 0:
    print("found")