[英]Python Pandas DataFrame Fill Missing Value
I have a dataframe like this with some NaNs: 我有一个这样的数据框,带有一些NaN:
df: df:
0 1
1 11.0 111.0
2 12.0 112.0
3 13.0 113.0
4 NaN 114.0
4 15.0 NaN
5 16.0 116.0
6 17.0 117.0
7 18.0 118.0
So what should I do to it to get the following: 所以我应该怎么做才能得到以下结果:
0 1
1 11.0 111.0
2 12.0 112.0
3 13.0 113.0
4 15.0 114.0
4 15.0 114.0
5 16.0 116.0
6 17.0 117.0
7 18.0 118.0
So that the NaN values in the index 4 are filled with index 4 values from other rows which are not NaN? 这样,索引4中的NaN值就会被其他不是NaN的行中的索引4值填充。
you can group by index and ffill()
+ bfill()
inside each group: 您可以在每个组内按索引和ffill()
+ bfill()
进行分组:
In [165]: df.groupby(level=0).apply(lambda x: x.ffill().bfill())
Out[165]:
0 1
1 11.0 111.0
2 12.0 112.0
3 13.0 113.0
4 15.0 114.0
4 15.0 114.0
5 16.0 116.0
6 17.0 117.0
7 18.0 118.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.