初学者与熊猫数据帧.我在下面的数据集中缺少A列和B列的值(Test.csv):
DateTime A B 01-01-2017 03:27 01-01-2017 03:28 01-01-2017 03:29 0.18127718 -0.178835737 01-01-2017 03:30 0.186923018 -0.183260853 01-01-2017 03:31 01-01-2017 03:32 01-01-2017 03:33 0.18127718 -0.178835737
我可以使用此代码使用向前传播填充值,但这仅适用于03:31和03:32,而不是03:27和03:28.
import pandas as pd import numpy as np df = pd.read_csv('test.csv', index_col = 0) data = df.fillna(method='ffill') ndata = data.to_csv('test1.csv')
结果是:
DateTime A B 01-01-2017 03:27 01-01-2017 03:28 01-01-2017 03:29 0.18127718 -0.178835737 01-01-2017 03:30 0.186923018 -0.183260853 01-01-2017 03:31 0.186923018 -0.183260853 01-01-2017 03:32 0.186923018 -0.183260853 01-01-2017 03:33 0.18127718 -0.178835737
我如何使用backfil包含'Bfill'来填补03:27和03:28的缺失值?
您可以使用ffill
并bfill
在需要时替换NaN
值向前和向后填充:
print (df) A B DateTime 01-01-2017 03:27 NaN NaN 01-01-2017 03:28 NaN NaN 01-01-2017 03:29 0.181277 -0.178836 01-01-2017 03:30 0.186923 -0.183261 01-01-2017 03:31 NaN NaN 01-01-2017 03:32 NaN NaN 01-01-2017 03:33 0.181277 -0.178836 data = df.ffill().bfill() print (data) A B DateTime 01-01-2017 03:27 0.181277 -0.178836 01-01-2017 03:28 0.181277 -0.178836 01-01-2017 03:29 0.181277 -0.178836 01-01-2017 03:30 0.186923 -0.183261 01-01-2017 03:31 0.186923 -0.183261 01-01-2017 03:32 0.186923 -0.183261 01-01-2017 03:33 0.181277 -0.178836
fillna
与带参数的函数相同:
data = df.fillna(method='ffill').fillna(method='bfill')