python - 我正在尝试用平均值替换 NaN 值
问题描述
我必须用 jupyter notebook 中的相应方法替换 s_months 和 events NaN 值。
输入数据 :
Types c_years o_periods s_months incidents
0 1 1 1 127.0 0.0
1 1 1 2 63.0 0.0
2 1 2 1 1095.0 3.0
3 1 2 2 1095.0 4.0
4 1 3 1 1512.0 6.0
5 1 3 2 3353.0 18.0
6 1 4 1 NaN NaN
7 1 4 2 2244.0 11.0
14 2 4 1 NaN NaN
我已经尝试了下面的代码,但它似乎不起作用,我尝试了不同的变体,例如替换转换。
df.fillna['s_months'] = df.fillna(df.grouby(['types' , 'o_periods']['s_months','incidents']).tranform('mean'),inplace = True)
s_months incidents
Types o_periods
1 1 911 3
2 1688 8
2 1 26851 36
2 14440 36
3 1 914 2
2 862 1
4 1 296 0
2 889 3
5 1 663 4
2 1046 6
解决方案
从你的DataFrame
:
>>> import pandas as pd
>>> from io import StringIO
>>> df = pd.read_csv(StringIO("""
Types,c_years,o_periods,s_months,incidents
0,1,1,1,127.0,0.0
1,1,1,2,63.0,0.0
2,1,2,1,1095.0,3.0
3,1,2,2,1095.0,4.0
4,1,3,1,1512.0,6.0
5,1,3,2,3353.0,18.0
6,1,4,1,NaN,NaN
7,1,4,2,2244.0,11.0
14,2,4,1,NaN,NaN"""), sep=',')
>>> df
Types c_years o_periods s_months incidents
0 1 1 1 127.0 0.0
1 1 1 2 63.0 0.0
2 1 2 1 1095.0 3.0
3 1 2 2 1095.0 4.0
4 1 3 1 1512.0 6.0
5 1 3 2 3353.0 18.0
6 1 4 1 NaN NaN
7 1 4 2 2244.0 11.0
14 2 4 1 NaN NaN
>>> df[['c_years', 's_months', 'incidents']] = df.groupby(['Types', 'o_periods']).transform(lambda x: x.fillna(x.mean()))
>>> df
Types c_years o_periods s_months incidents
0 1 1 1 127.000000 0.0
1 1 1 2 63.000000 0.0
2 1 2 1 1095.000000 3.0
3 1 2 2 1095.000000 4.0
4 1 3 1 1512.000000 6.0
5 1 3 2 3353.000000 18.0
6 1 4 1 911.333333 3.0
7 1 4 2 2244.000000 11.0
14 2 4 1 NaN NaN
最后一个NaN
在这里是因为它属于最后一个在列中不包含任何值的组,s_months
因此incidents
不包含mean
.
推荐阅读
- python - Python ElementTree XML:添加外部链接
- ruby-on-rails - 在 Rails 数据库中保存用户的 pushSubscription 信息?
- configuration - WOSERVICEADDRESS X & Y:区分来源(SA 与用户定义)
- c# - 在进一步处理之前实现比较两个字符串的逻辑
- python - 需要在项目根目录之外有烧瓶静态文件夹
- c# - 如何将一个滚动条组合到多个富文本框
- oauth-2.0 - 从 intuit quickbooks 获取令牌时的 InvalidFieldFormat
- java - 如何从 URL 获取动态查询文本?
- regex - 正则表达式仅最后匹配
- excel - 当代码因 VBA 错误而失败时,如何保护工作表?