我试图确定Pandas列中是否有一个具有特定值的条目。我试图用来做到这一点if x in df['id']。我以为这是行得通的,除非当我向它提供一个我不知道的值时,43 in df['id']它仍然返回True。当我将一个数据帧的子集仅包含与缺少的ID匹配的条目时df[df['id'] == 43],很显然,其中没有任何条目。如何确定Pandas数据框中的列是否包含特定值,为什么我的当前方法不起作用?(仅供参考,当我在类似问题的答案中使用实现时,也会遇到相同的问题)。
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).
回答 0
in 系列的值检查值是否在索引中:
In[11]: s = pd.Series(list('abc'))In[12]: s
Out[12]:0 a
1 b
2 c
dtype: object
In[13]:1in s
Out[13]:TrueIn[14]:'a'in s
Out[14]:False
In[2]: s = pd.Series(list('abc'))In[3]: s
Out[3]:0 a
1 b
2 c
dtype: object
In[3]: s.isin(['a'])Out[3]:0True1False2False
dtype: bool
In[4]: s[s.isin(['a'])].empty
Out[4]:FalseIn[5]: s[s.isin(['z'])].empty
Out[5]:True
You can also use pandas.Series.isin although it’s a little bit longer than 'a' in s.values:
In [2]: s = pd.Series(list('abc'))
In [3]: s
Out[3]:
0 a
1 b
2 c
dtype: object
In [3]: s.isin(['a'])
Out[3]:
0 True
1 False
2 False
dtype: bool
In [4]: s[s.isin(['a'])].empty
Out[4]: False
In [5]: s[s.isin(['z'])].empty
Out[5]: True
But this approach can be more flexible if you need to match multiple values at once for a DataFrame (see DataFrame.isin)
>>> df = DataFrame({'A': [1, 2, 3], 'B': [1, 4, 7]})
>>> df.isin({'A': [1, 3], 'B': [4, 7, 12]})
A B
0 True False # Note that B didn't match 1 here.
1 False True
2 True True
回答 2
found = df[df['Column'].str.contains('Text_to_search')]print(found.count())
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 [15]: timeit x.isin([999999]).any()
9.54 ms ± 291 µs per loop (mean ± std. dev. of 7 runs, 100 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 [17]: timeit 999999 in set(x)
79.8 ms ± 1.98 ms per loop (mean ± std. dev. of 7 runs, 10 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 [24]: timeit 9 in x.values
666 µs ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Interestingly it doesn’t matter if you look up 9 or 999999, it seems like it takes about the same amount of time using the in syntax (must be using binary search)
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)
Seems like using x.values is the fastest, but maybe there is a more elegant way in pandas?
回答 4
或使用Series.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
>>> s = pd.Series(list('abc'))
>>> s
0 a
1 b
2 c
dtype: object
>>> 'a' in s.tolist()
True
>>> (s=='a').any()
True
Series.tolist makes a list about of a Series, and the other one i am just getting a boolean Series from a regular Series, then checking if there are any Trues in the boolean Series.
回答 5
简单条件:
if any(str(elem)in['a','b']for elem in df['column'].tolist()):