Series.isin(selfvalues)

  • values: 一个集合或列表

DataFrame.isin(selfvalues

  • values: 可以是可迭代对象,Series,DataFrame或者是字典

两个操作是一样的,只是参数返回值不同,一个是Series,另一个是DataFrame。

Series Examples

>>> s = pd.Series(['lama', 'cow', 'lama', 'beetle', 'lama',
               'hippo'], name='animal')
>>> s.isin(['cow', 'lama'])
0     True
1     True
2     True
3    False
4     True
5    False
Name: animal, dtype: bool

>>> s.isin(['lama'])
0     True
1    False
2     True
3    False
4     True
5    False
Name: animal, dtype: bool

DataFrame Examples

>>> df = pd.DataFrame({'num_legs': [2, 4], 'num_wings': [2, 0]},
                  index=['falcon', 'dog'])
>>> df
        num_legs  num_wings
falcon         2          2
dog            4          0

>>> df.isin([0, 2])
        num_legs  num_wings
falcon      True       True
dog        False       True

>>> df.isin({'num_wings': [0, 3]})
        num_legs  num_wings
falcon     False      False
dog        False       True

>>> other = pd.DataFrame({'num_legs': [8, 2], 'num_wings': [0, 2]},
                     index=['spider', 'falcon'])
>>> df.isin(other)
        num_legs  num_wings
falcon      True       True
dog        False      False
Categories: pandasPython

1 Comment

Pandas – Indexing and selecting data – 心自彷徨的窝窝~ · 2020年3月25日 at 13:23

[…] 见链接 pandas.Series.isin()和pandas.DataFrame.isin() […]

Leave a Reply

Your email address will not be published.