Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a value exist in a Pandas Series using 'in'

Tags:

python

pandas

I am trying to check if a value exists in a pandas series, However, I found an interesting fact:

s1 = pd.Series(['one', 'one1', '1', ''])
'one' in s1
False

'one' in set(s1)
True

Why wouldn't the in operation work for the Series object? Thanks!

like image 407
Kaihua Hou Avatar asked Sep 12 '25 18:09

Kaihua Hou


1 Answers

in checks if index is in this series

In[29]: s1.__contains__
Out[29]: 
<bound method NDFrame.__contains__ of 
0     one
1    one1
2       1
3        
dtype: object>

In[30]: 'one' in s1
Out[30]: False

In[31]: 0 in s1
Out[31]: True

In[32]: 1 in s1
Out[32]: True

In[33]: 2 in s1
Out[33]: True

In[34]: 3 in s1
Out[34]: True

In[35]: 4 in s1
Out[35]: False
like image 94
Xixiaxixi Avatar answered Sep 14 '25 08:09

Xixiaxixi