2024-06-09 23:25:10 发布
网友
用共享相同类型和相同运行周期的其他船舶的相应方法替换变量“s_月”和“事件”中缺失的值。 这里的“s_月”和“事件”是两列,具有nan值,我们希望填充它们
我试着根据要求的条件找到办法。但无法在ship数据帧中填充nan值。这里是作为数据帧计算和存储的平均值
使用groupby和combine_first填充NaN:
groupby
combine_first
Minimal Reproducible Example:
>>> df types o_periods s_months incidents 0 1 2 63.0 0.0 1 1 2 1095.0 4.0 2 1 2 3353.0 18.0 3 1 2 NaN NaN
keys = ['types', 'o_periods'] vals = ['s_months', 'incidents'] df[vals] = df[vals].combine_first(df.groupby(keys)[vals].transform('mean'))
输出结果:
>>> df types o_periods s_months incidents 0 1 2 63.000000 0.000000 1 1 2 1095.000000 4.000000 2 1 2 3353.000000 18.000000 3 1 2 1503.666667 7.333333
使用
groupby
和combine_first
填充NaN:Minimal Reproducible Example:
输出结果:
相关问题 更多 >
编程相关推荐