Python Pandas实现数据分组求平均值并填充nan的示例
作者:好男孩朋友99 时间:2021-03-24 11:51:30
Python实现按某一列关键字分组,并计算各列的平均值,并用该值填充该分类该列的nan值。
DataFrame数据格式
fillna方式实现
groupby方式实现
DataFrame数据格式
以下是数据存储形式:
fillna方式实现
1、按照industryName1列,筛选出业绩
2、筛选出相同行业的Series
3、计算平均值mean,采用fillna函数填充
4、append到新DataFrame中
5、循环遍历行业名称,完成2,3,4步骤
factordatafillna = pd.DataFrame()
industrys = newfactordata1.industryName1.unique()
for ind in industrys:
t = newfactordata1.industryName1 == ind
a = newfactordata1[t].fillna(newfactordata1[t].mean())
factordatafillna = factordatafillna.append(a)
groupby方式实现
采用groupby计算,详细见代码注释
df = pd.DataFrame({'code':[1,2,3,4,5,6,7,8],
'value':[np.nan,5,7,8,9,10,11,12],
'value2':[5,np.nan,7,np.nan,9,10,11,12],
'indstry':['农业1','农业1','农业1','农业2','农业2','农业4','农业2','农业3']},
columns=['code','value','value2','indstry'],
index=list('ABCDEFGH'))
# 只留下需要处理的列
cols = [col for col in df.columns if col not in['code','indstry']]
# 分组的列
gp_col = 'indstry'
# 查询nan的列
df_na = df[cols].isna()
# 根据分组计算平均值
df_mean = df.groupby(gp_col)[cols].mean()
print(df)
# 依次处理每一列
for col in cols:
na_series = df_na[col]
names = list(df.loc[na_series,gp_col])
t = df_mean.loc[names,col]
t.index = df.loc[na_series,col].index
# 相同的index进行赋值
df.loc[na_series,col] = t
print(df)
code value value2 indstry
A 1 NaN 5.0 农业1
B 2 5.0 NaN 农业1
C 3 7.0 7.0 农业1
D 4 8.0 NaN 农业2
E 5 9.0 9.0 农业2
F 6 10.0 10.0 农业4
G 7 11.0 11.0 农业2
H 8 12.0 12.0 农业3
code value value2 indstry
A 1 6.0 5.0 农业1
B 2 5.0 6.0 农业1
C 3 7.0 7.0 农业1
D 4 8.0 10.0 农业2
E 5 9.0 9.0 农业2
F 6 10.0 10.0 农业4
G 7 11.0 11.0 农业2
H 8 12.0 12.0 农业3
来源:https://blog.csdn.net/u010383605/article/details/78879515#groupby方式实现
标签:Python,Pandas,平均值,填充,nan
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python设计模式中的结构型适配器模式
2023-07-19 11:59:54
![](https://img.aspxhome.com/file/2023/7/59657_0s.png)
写给应聘页面重构的同学
2009-03-18 11:01:00
Python正规则表达式学习指南
2021-04-11 15:21:16
![](https://img.aspxhome.com/file/2023/7/68827_0s.png)
如何利用SysOjects来获知数据库的信息?
2010-01-01 15:43:00
python 使用OpenCV进行简单的人像分割与合成
2021-06-16 20:46:39
![](https://img.aspxhome.com/file/2023/8/75128_0s.png)
2个asp防刷新程序代码
2008-09-28 21:16:00
php中去除所有js,html,css代码
2023-07-08 07:59:52
JavaScript中Infinity(无穷数)的使用和注意事项
2023-08-21 00:59:13
如何使用python写截屏小工具
2021-04-04 23:51:14
![](https://img.aspxhome.com/file/2023/7/86207_0s.gif)
解决pygal.style的LightColorizedStyle参数问题
2023-09-20 06:39:02
![](https://img.aspxhome.com/file/2023/3/84113_0s.png)
Python统计词频并绘制图片(附完整代码)
2022-01-01 09:28:24
![](https://img.aspxhome.com/file/2023/7/110447_0s.jpg)
浅析MySQL数据库授权原则
2009-12-15 09:21:00
Centos7下编译安装配置Nginx+PHP+MySql环境
2023-11-14 19:10:06
python爬取cnvd漏洞库信息的实例
2021-08-24 07:18:06
![](https://img.aspxhome.com/file/2023/3/79633_0s.jpg)
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析
2022-08-29 11:18:20
![](https://img.aspxhome.com/file/2023/8/102718_0s.png)
将滚动条(scrollbar)保持在最底部的方法
2008-02-21 10:05:00
树型结构在ASP中的简单解决
2007-10-07 12:52:00
解决PyCharm中光标变粗的问题
2022-10-24 15:47:32
![](https://img.aspxhome.com/file/2023/9/105739_0s.png)
pyqt5实现绘制ui,列表窗口,滚动窗口显示图片的方法
2023-03-22 16:52:56
![](https://img.aspxhome.com/file/2023/2/112472_0s.jpg)
【总结】ASP如何获取访客真实的IP地址
2007-08-15 13:43:00