简体   繁体   English

用熊猫填充数据框中的NaN值

[英]Fill NaN values in dataframe with pandas

I have a dataframe that I created from a text file. 我有一个从文本文件创建的数据框。 Columns BF should apply to all null fields below them, then once all nulls are filled the next set of periods should be filled by the next values populated in BF. BF列应应用于它们下面的所有空字段,然后,一旦所有空值都被填充,下一组时间段应由BF中填充的下一个值填充。 How would I go about accomplishing this? 我将如何实现这一目标?

数据框截图

You'll want to use the fillna() method of DataFrames, with a forward-fill: 您将要使用fillna()方法,并进行正向填充:

df.fillna(method='ffill')

Here's a quick example: 这是一个简单的示例:

df = pd.DataFrame({'A': [4, None, None, 5, None, None],
                   'B': [2, None, None, 3, None, None],
                   'C': range(6)})

>>> df
    A   B  C
0   4   2  0
1 NaN NaN  1
2 NaN NaN  2
3   5   3  3
4 NaN NaN  4
5 NaN NaN  5

>>> df.fillna(method='ffill')
   A  B  C
0  4  2  0
1  4  2  1
2  4  2  2
3  5  3  3
4  5  3  4
5  5  3  5

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

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