在使用Pandas处理数据的时候经常遇到重复数据的情况,这时候可以使用drop_duplicates(pandas.DataFrame.drop_duplicates)来进行处理。函数定义如下:
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)[source]
返回去重后的数据集(如果inplace为True则返回None)
参数:
subset:column label or sequence of labels, optional
Only consider certain columns for identifying duplicates, by default use all of the columns.
用来指定特定的列,默认所有列
keep:{‘first’, ‘last’, False}, default ‘first’
Determines which duplicates (if any) to keep. - first : Drop duplicates except for the first occurrence. - last : Drop duplicates except for the last occurrence. - False : Drop all duplicates.
确定保留哪条重复的数据,默认第一条
inplace:bool, default False
Whether to drop duplicates in place or to return a copy.
是直接在原来数据上修改还是保留一个副本
ignore_index:bool, default False
If True, the resulting axis will be labeled 0, 1, …, n - 1.
是否忽略Index,默认为True
例子:
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
By default, it removes duplicate rows based on all columns.
df.drop_duplicates()
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
To remove duplicates on specific column(s), use subset.
具体可以参考说明:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop_duplicates.html
2021年3月24日,主题为《数据的世界,世界的数据》的星环科技2021春季新品发布会...
Docker生成新镜像版本的两种方式 There are two ways Docker can generate new m...
从 10.0.0 版开始,异步迭代器就出现在 Node 中了,在本文中,我们将讨论异步迭...
本文整理自直播《Hologres 数据导入/导出实践-王华峰(继儒)》 视频链接: https:/...
摘要 元旦期间 订单业务线 告知 推送系统 无法正常收发消息,作为推送系统维护者...
【51CTO.com快译】 数据可视化工具不断发展,提供更强大的功能,同时改善可访问...
信息化2.0时代提出开展智慧教育创新发展行动。2019年2月,中共中央、国务院印发...
建站 什么 虚拟主机 够用?这要看搭建的是什么类型的网站。比如个人博客类型的网...
在Python语言中有如下3种方法: 成员方法 类方法(classmethod) 静态方法(staticm...
前提条件 请您在购买前确保已完成注册和充值。详细操作请参见 如何注册公有云管...