Python Pandas — why does the `in` operator work with indices and not with the data?

前端 未结 1 1416
情书的邮戳
情书的邮戳 2020-12-20 10:27

I discovered the hard way that Pandas in operator, applied to Series operates on indices and not on the actual data:

In [1]: import         


        
相关标签:
1条回答
  • 2020-12-20 10:54

    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}
    >>> 1 in d
    False
    >>> 'a' in d
    True
    

    with:

    >>> s = pandas.Series([1], index=['a'])
    >>> 1 in 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.

    0 讨论(0)
提交回复
热议问题