我有一个如下所示的DataFrame,其中Diff列是Value - PreviousValue。
Date Value Diff
2010-01-01 100 Na
2010-02-01 110 10
2010-03-01 130 20
2010-04-01 100 -30
2010-05-01 Na 20
2010-06-01 Na 30
正如您所看到的,即使我们有Diff,也会丢失一些值。我希望重建表格以使值正确:
Date Value Diff
2010-01-01 100 Na
2010-02-01 110 10
2010-03-01 130 20
2010-04-01 100 -30
2010-05-01 120 20
2010-06-01 150 30
如何做到这一点?
答案 0 :(得分:1)
通过使用fillna
和ffill
cumsum
In [1525]: df['Value'].fillna(df['Value'].ffill() + df['Diff'].cumsum())
Out[1525]:
0 100.0
1 110.0
2 130.0
3 100.0
4 120.0
5 150.0
Name: Value, dtype: float64
In [1526]: df['Value'] = df['Value'].fillna(df['Value'].ffill() + df['Diff'].cumsum())
In [1527]: df
Out[1527]:
Date Value Diff
0 2010-01-01 100.0 NaN
1 2010-02-01 110.0 10.0
2 2010-03-01 130.0 20.0
3 2010-04-01 100.0 -30.0
4 2010-05-01 120.0 20.0
5 2010-06-01 150.0 30.0