语法
DataFrame.duplicated(subset=None, keep='first')
详情见官方(https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.duplicated.html)
例子
>>> df = pd.DataFrame({
'brand': ['YumYum','YumYum', 'YumYum', 'Indomie', 'Indomie', 'Indomie'],
'style': ['cup','cup', 'cup', 'cup', 'pack', 'pack'],
'rating': [4, 4, 4, 3.5, 15, 5]})
>>> df
brand style rating
0 YumYum cup 4.0
1 YumYum cup 4.0
2 YumYum cup 4.0
3 Indomie cup 3.5
4 Indomie pack 15.0
5 Indomie pack 5.0
默认情况下,对于每一组重复的值,第一次出现的值设置为False,其他所有值设置为True
>>> df.duplicated()
0 False
1 True
2 True
3 False
4 False
5 False
dtype: bool
>>> type(df.duplicated())
<class 'pandas.core.series.Series'>
通过使用' last ',每组重复值的最后一次出现被设置为False,而其他所有重复值被设置为True。
>>> df.duplicated(keep='last')
0 True
1 True
2 False
3 False
4 False
5 False
dtype: bool
使用子subset查找特定列上的重复项。
>>> df.duplicated(subset=['brand'])
0 False
1 True
2 True
3 False
4 True
5 True
dtype: bool
通过将keep设置为False,所有重复项都为True。
>>> df.duplicated(keep=False)
0 True
1 True
2 True
3 False
4 False
5 False
dtype: bool