2018年4月3日 星期二

Python - 在Pandas DataFrame中缺失資料的處理-以上一個row的值來填補 - How to replace NaN(None,Null) by preceding values in pandas DataFrame?

Information:

System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

Code:

df1 = pd.DataFrame([[3, 1, 8], [None, 5, None], [None, None, 9]])
df1_fillna = df1.fillna(method='ffill')
print('df1')
print(df1)
print('='*20)
print('df1_fillna')
print(df1_fillna)

Result:

df1
     0    1    2
0  3.0  1.0  8.0
1  NaN  5.0  NaN
2  NaN  NaN  9.0
====================
df1_fillna
     0    1    2
0  3.0  1.0  8.0
1  3.0  5.0  8.0
2  3.0  5.0  9.0

沒有留言:

張貼留言