1
我想根据列值的平均值填写NaN。 例如:填写Nan基于组
Groups Temp
1 5 27
2 5 23
3 5 NaN (will be replaced by 25)
4 1 NaN (will be replaced by the mean of the Temps that are in group 1)
有什么建议吗?谢谢 !
我想根据列值的平均值填写NaN。 例如:填写Nan基于组
Groups Temp
1 5 27
2 5 23
3 5 NaN (will be replaced by 25)
4 1 NaN (will be replaced by the mean of the Temps that are in group 1)
有什么建议吗?谢谢 !
使用groupby
,transfrom
,并与fillna
和mean
lamdba功能:
df = df.assign(Temp=df.groupby('Groups')['Temp'].transform(lambda x: x.fillna(x.mean())))
print(df)
输出:
Temp
0 27.0
1 23.0
2 25.0
3 NaN
我只是删除我的回答了相同的反正:) – Vaishali
我还有其他的比列温度和群体,我怎么才能精确到只有在tempna? – hdatas
真棒谢谢! – hdatas