在DataFrame中插入带有上一行差异的新列?

时间:2019-01-23 22:05:40

标签: python pandas

我有一个带有数据集的Pandas DataFrame,看起来像这样:

                           floor_temperature  power_consumption  outside_temperature
timestamp
2019-01-23 00:00:00+00:00           8.300000           0.022000           -11.393333
2019-01-23 00:10:00+00:00           8.114286           0.016000           -11.400000
2019-01-23 00:20:00+00:00           7.950000           0.044000           -11.254545
2019-01-23 00:30:00+00:00           7.900000           0.027500           -11.300000
2019-01-23 00:40:00+00:00           7.840000         830.900000           -11.400000
2019-01-23 00:50:00+00:00           8.300000         334.352000           -11.400000
2019-01-23 01:00:00+00:00           8.580000           0.028000           -11.380000
2019-01-23 01:10:00+00:00           8.440000           0.018000           -11.400000
2019-01-23 01:20:00+00:00           8.360000           0.022000           -11.400000

我想在“ floor_temperatur”之后插入一个新行,该行应命名为“ diff_floor_temperature”,并且应通过减去“ floor_temperature”行减去之前的“ floor_temperature”行来计算。

我知道使用.diff()函数应该很容易,但是在语法上有一些帮助会很好。

1 个答案:

答案 0 :(得分:1)

将0x5453的答案移至“答案”列,但要进行一些更改以确保不会出现任何切片错误。

df.loc[:,'diff_floor_temperature'] = df.loc[:,'floor_temperature'].diff()