简体   繁体   English

Python Pandas 基于另一个对应数据框合并两个数据框

[英]Python Pandas Merge Two Dataframes Based on Another Correspondence Dataframe

I'm working with 2 dataframes.我正在使用 2 个数据框。 Dataframe1 is for parking sites. Dataframe1 用于停车位。 Dataframe2 is for sensors. Dataframe2 用于传感器。 Correspondence dataframe shows which sensor is in which site.对应数据框显示哪个传感器位于哪个站点。

Dataframe1:数据框1:

     Site   Time   Available   Capacity
0    19E    12:00  5           10
1    19E    13:00  4           10 
2    44E    12:00  8           22 
3    44E    13:00  11          22 

Dataframe2:数据框2:

     Sensor   Time   Temp   Precipitation
0    113      12:00  74     0.01
1    113      13:00  76     0.02 
2    114      12:00  75     0.00 
3    114      13:00  77     0.00 

Correspondence dataframe:对应数据框:

     Site   Sensor  
0    19E    113
1    44E    114
2    58E    115
... 

I'd like to combine dataframe 1 and 2 based on the correspondence dataframe, and also 'Time' column.我想根据对应数据框以及“时间”列组合数据框 1 和 2。 Intervals are both 1h in those two dataframes.这两个数据帧中的间隔都是 1h。

Expected result:预期结果:

     Site   Time   Available   Capacity   Sensor   Time   Temp   Precipitation
0    19E    12:00  5           10         113      12:00  74     0.01
1    19E    13:00  4           10         113      13:00  76     0.02
2    44E    12:00  8           22         114      12:00  75     0.00
3    44E    13:00  11          22         114      13:00  77     0.00 

You can use the code below to generate raw materials:您可以使用以下代码生成原材料:

import pandas as pd

df1 = pd.DataFrame({
    'Site': {0: '19E', 1: '19E', 2: '44E', 3: '44E'},
    'Time': {0: '12:00', 1: '13:00', 2: '12:00', 3: '13:00'},
    'Available': {0: 5, 1: 4, 2: 8, 3: 11},
    'Capacity': {0: 10, 1: 10, 2: 22, 3: 22}})

df2 = pd.DataFrame({
    'Sensor': {0: 113, 1: 113, 2: 114, 3: 114},
    'Time': {0: '12:00', 1: '13:00', 2: '12:00', 3: '13:00'},
    'Tem': {0: 74, 1: 76, 2: 75, 3: 77},
    'Precipitation': {0: 0.01, 1: 0.02, 2: 0.00, 3: 0.00}})

cor_df = pd.DataFrame({
    'Site': {0: '19E', 1: '44E', 2: '58E'},
    'Sensor': {0: 113, 1: 114, 2: 115}})

Use Series.map to map Site to Sensor and then DataFrame.merge on Sensor and Time :使用Series.map映射SiteSensor ,然后DataFrame.mergeSensorTime

lookup = cor_df.set_index("Site").squeeze()
res = df1.assign(Sensor=df1["Site"].map(lookup)).merge(df2, on=["Sensor", "Time"])
print(res)

Output输出

  Site   Time  Available  Capacity  Sensor  Tem  Precipitation
0  19E  12:00          5        10     113   74           0.01
1  19E  13:00          4        10     113   76           0.02
2  44E  12:00          8        22     114   75           0.00
3  44E  13:00         11        22     114   77           0.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM