首页 > 解决方案 > 在 pandas 中查找两个不匹配的数据帧的增量

问题描述

有 2 个数据帧,在 2 个不同时间有读数:

DF1

       Sensor ID    Reference Pressure     Sensor Pressure
0         013677                100.15               93.18
1         013688                101.10               95.23
2         013699                100.87               93.77
...          ...                  ...                  ...

DF2

       Sensor ID    Reference Pressure     Sensor Pressure
0         013688                120.01              119.43
1         013677                118.93              118.88
2         013699                120.05              118.85
...          ...                  ...                  ...

鉴于“传感器 ID”值顺序在两个数据帧之间不匹配,创建包含这些读数之间差异的第三个数据帧的最佳方法是什么?

标签: pythonpandas

解决方案


Pandas 有这个漂亮的功能,它可以自动对齐索引。所以我们可以用它来解决你的问题:

df1.set_index("Sensor ID").sub(df2.set_index("Sensor ID"))
           Reference Pressure  Sensor Pressure
Sensor ID                                     
13677                  -18.78           -25.70
13688                  -18.91           -24.20
13699                  -19.18           -25.08

推荐阅读