python - 在 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() 函数应该很容易,但是在语法上提供一些帮助会很好。
解决方案
将 0x5453 的答案移至答案列,但更改了一些内容以确保不会出现任何切片错误。
df.loc[:,'diff_floor_temperature'] = df.loc[:,'floor_temperature'].diff()
推荐阅读
- java - Selenium 如何多次等待具有不同文本的同一个 WebElement
- ios - 尝试在表格视图中配置两个单元格时出现“致命错误:索引超出范围”错误
- kubernetes - 如何访问 kubectl 返回的映射中的键
- angular - Angular 8,IE11 启动时出错 Function.prototype.toString: 'this' is not a Function object
- ios - 我可以将 iOS 图形 API 设置为专门针对 iPhone 6s 的“OpenGLES2”,而将其他所有内容设置为自动吗?
- python - 如何更改 Pytorch 预训练模块中的激活层?
- powershell - 如何在powershell中使用where条件
- python - 在 Pandas Dataframe 中用另一列替换一列
- c++ - 在 OnOK 中保存对话框数据时出现问题
- sql - 从 SQL 查询返回 xml 数据