pandas.DataFrame.duplicated#
- DataFrame.duplicated(subset=None, keep='first')[源代码]#
返回表示重复行的布尔 Series。
考虑特定列是可选的。
- 参数:
- subset列标签或标签的可迭代对象,可选
仅考虑特定列来识别重复项,默认为使用所有列。
- keep{‘first’, ‘last’, False}, 默认为 ‘first’
确定要标记哪些重复项(如果有)。
first: 标记重复项为True,但第一个出现项除外。last: 标记重复项为True,但最后一个出现项除外。False : 标记所有重复项为
True。
- 返回:
- Series
每个重复行的布尔序列。
另请参阅
Index.duplicated索引上的等效方法。
Series.duplicatedSeries 上的等效方法。
Series.drop_duplicates从 Series 中删除重复值。
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