啊啊啊啊啊吖

2019-01-24   阅读量: 1714

数据分析师 Python数据分析

使用前一行的diff在DataFrame中插新的行

扫码加入数据分析学习群

我有一个带有数据集的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()函数应该很容易,但是对语法有一些帮助会很好。

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
46.1538 1 3 关注作者 收藏

评论(1)

啊啊啊啊啊吖
2019-01-24

将0x5453的答案移至答案列,但更改了一些内容以确保不会出现任何切片错误。

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

0.0000 0 0 回复

推荐课程