pandas.DataFrame.duplicated用法

语法

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

 

  • 13
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值