熊猫:从差异列

时间:2017-09-20 06:25:48

标签: pandas

我有一个如下所示的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

如何做到这一点?

1 个答案:

答案 0 :(得分:1)

通过使用fillnaffill

对值进行反向解码,使用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
相关问题