Python Pandas -- Why Does The `in` Operator Work With Indices And Not With The Data?
I discovered the hard way that Pandas in operator, applied to Series operates on indices and not on the actual data: In [1]: import pandas as pd In [2]: x = pd.Series([1, 2, 3])
Solution 1:
It is may be helpful to think of the pandas.Series
as being a bit like a dictionary, where the index
values are equivalent to the keys
. Compare:
>>>d = {'a': 1}>>>1in d
False
>>>'a'in d
True
with:
>>>s = pandas.Series([1], index=['a'])>>>1in s
False
>>>'a'in s
True
However, note that iterating over the series iterates over the data
, not the index
, so list(s)
would give [1]
, not ['a']
.
Indeed, per the documentation, the index
values "must be unique and hashable", so I'd guess there's a hashtable under there somewhere.
Post a Comment for "Python Pandas -- Why Does The `in` Operator Work With Indices And Not With The Data?"