简体   繁体   English

使用索引填充pandas数据帧中的平均值

[英]Using indexes to fill mean values in pandas dataframe

样本数据

Trying to populate the mean values in a dataframe having NaN values. 尝试在具有NaN值的数据框中填充平均值。 Need to populate the dataframe only for certain rows and not all rows. 需要仅为某些行填充数据框,而不是所有行。

Sample Data set 样本数据集

   A    b     c    
0 100   52    555
1 25   144    552
2 NaN  555    555
3 323  NaN    785
4 515  551    125
5 741  471    Nan
6 111  Nan    Nan

Here I need to fill values only for rows numbered 5 and 6 with the mean of their respective columns and should ignore rows 3 and 4. 在这里,我需要仅为编号为5和6的行填充其各自列的平均值,并且应忽略第3行和第4行。

您可以在数据帧切片上使用fillna()函数

df.iloc[5:7].fillna(mean, inplace=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM