pandas.DataFrame.isin#

DataFrame.isin(values)[源代码]#

DataFrame中的每个元素是否包含在值中。

参数
values可迭代、系列、数据帧或DICT

只有在所有标签都匹配的情况下,结果才会在某个位置为真。如果 values 是一个系列,这就是索引。如果 values 是词典,则键必须是列名,列名必须匹配。如果 values 是DataFrame,则索引和列标签必须匹配。

退货
DataFrame

布尔值的DataFrame,显示DataFrame中的每个元素是否包含在值中。

参见

DataFrame.eq

DataFrame的相等性测试。

Series.isin

级数的等价法。

Series.str.contains

测试序列或索引的字符串中是否包含模式或正则表达式。

示例

>>> 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

什么时候 values 是检查DataFrame中的每个值是否都出现在列表中的列表(哪些动物有0条或2条腿或2个翅膀)

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

检查是否 valuesnot 在DataFrame中,使用 ~ 操作员:

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

什么时候 values 是一个字典,我们可以分别为每一列传递要检查的值:

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

什么时候 values 是系列或DataFrame,索引和列必须匹配。请注意,根据Other中的腿数,‘Falcon’不匹配。

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