pandas去重复行并分类汇总的实现方法
作者:qq_24499417 时间:2021-06-12 17:38:37
今天主要记录一下pandas去重复行以及如何分类汇总。以下面的数据帧作为一个例子:
import pandas as pd
data=pd.DataFrame({'产品':['A','A','A','A'],'数量':[50,50,30,30]})
pandas判断dataframe是否含有重复行数据用:df.duplicated()
第一次出现的数据为False.重复的数据行就被记录为True。
去掉重复行数据使用data.drop_duplicates().
可以看到索引乱了,我们使用data.reset_index(),里面的参数drop=True,表明要舍掉原来的索引,不然的话原来的索引会保留下来。
分类汇总主要使用groupby(表明汇总的条件列)以及agg(要汇总的字段/列以及汇总的方式:求和还是最大最小值或者计数)。完整代码如下图
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 20 09:08:10 2018
@author: FanXiaoLei
"""
import pandas as pd
data=pd.DataFrame({'产品':['A','A','A','A'],'数量':[50,50,30,30]})
if data.duplicated:
dataA=data.drop_duplicates().reset_index(drop=True)
print(dataA)
dataB=dataA.groupby(by='产品').agg({'数量':sum})
print('数据汇总结果:')
print(dataB)
结果展示如下图:
来源:https://blog.csdn.net/qq_24499417/article/details/81126807
标签:pandas,重复行,分类汇总
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何获知文件最后的修改日期和时间?
2009-11-24 20:49:00
python用装饰器自动注册Tornado路由详解
2021-07-16 07:53:37
用err.raise自定义错误信息
2008-02-20 12:48:00
Python 使用input同时输入多个数的操作
2023-09-23 22:36:42
![](https://img.aspxhome.com/file/2023/8/107138_0s.jpg)
python中的sys模块和os模块
2021-07-06 00:43:17
![](https://img.aspxhome.com/file/2023/6/76836_0s.png)
python制作抖音代码舞
2021-09-05 21:51:00
![](https://img.aspxhome.com/file/2023/8/105088_0s.jpg)
Python实现简单的学生信息管理系统
2021-06-19 10:59:44
![](https://img.aspxhome.com/file/2023/6/82776_0s.jpg)
python中字符串变二维数组的实例讲解
2021-08-03 04:37:56
![](https://img.aspxhome.com/file/2023/5/76885_0s.jpg)
pytorch实现textCNN的具体操作
2022-08-28 17:40:00
![](https://img.aspxhome.com/file/2023/9/79569_0s.png)
再谈Python中的字符串与字符编码(推荐)
2023-06-15 23:25:08
![](https://img.aspxhome.com/file/2023/5/81445_0s.png)
Python面试之os.system()和os.popen()的区别详析
2023-09-14 18:15:39
Python高光谱遥感影像处理问题详细分析讲解
2023-10-04 04:03:37
Scrapy元素选择器Xpath用法汇总
2021-09-11 11:42:58
CSS的书写顺序规范
2008-06-12 13:51:00
Python画图高斯分布的示例
2023-02-07 09:09:14
Python安装第三方库的3种方法
2022-02-03 03:10:47
pandas分批读取大数据集教程
2023-01-13 16:45:32
![](https://img.aspxhome.com/file/2023/8/89398_0s.jpg)
JS:firefox 兼容性问题
2009-08-01 16:20:00
python实现word 2007文档转换为pdf文件
2021-05-27 20:57:54
作为Web开发人员,我为什么喜欢Google Chrome浏览器
2011-08-29 15:37:47