pandas.DataFrame.duplicated#
- DataFrame.duplicated(subset=None, keep='first')[源代码]#
返回表示重复行的布尔系列。
考虑某些列是可选的。
- 参数
- subset列标签或标签序列,可选
只考虑某些列来标识重复项,默认情况下使用所有列。
- keep{‘First’,‘Last’,False},默认为‘First’
确定要标记的副本(如果有)。
first
:将重复项标记为True
除了第一次出现的情况。last
:将重复项标记为True
除了最后一次出现。False:将所有重复项标记为
True
。
- 退货
- 系列
每个重复行的布尔级数。
参见
Index.duplicated
指数的等价法。
Series.duplicated
级数的等价法。
Series.drop_duplicates
从系列中删除重复值。
DataFrame.drop_duplicates
从DataFrame中删除重复值。
示例
考虑包含拉面评级的数据集。
>>> df = pd.DataFrame({ ... 'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'], ... 'style': ['cup', 'cup', 'cup', 'pack', 'pack'], ... 'rating': [4, 4, 3.5, 15, 5] ... }) >>> df brand style rating 0 Yum Yum cup 4.0 1 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.0
默认情况下,对于每组重复的值,第一个匹配项设置为False,所有其他匹配项设置为True。
>>> df.duplicated() 0 False 1 True 2 False 3 False 4 False dtype: bool
通过使用‘last’,每个重复值集的最后一个匹配项被设置为False,所有其他值都设置为True。
>>> df.duplicated(keep='last') 0 True 1 False 2 False 3 False 4 False dtype: bool
通过设置
keep
如果为False,则所有重复项均为True。>>> df.duplicated(keep=False) 0 True 1 True 2 False 3 False 4 False dtype: bool
要查找特定列上的重复项,请使用
subset
。>>> df.duplicated(subset=['brand']) 0 False 1 True 2 False 3 True 4 True dtype: bool