我有2个不同的数据帧
DF1
ID Datetime Event
1 07:51 XX
2 08:50 XY
3 09:01 XZ
DF2
ID Datetime Value
1 07:55 AB
2 08:54 BC
3 09:05 CD
我需要比较两个数据帧之间的ID,然后DF1的日期时间将小于DF2,然后事件列就会出现。
DF2
ID Datetime Value Event
1 07:55 AB XX
2 08:54 BC XY
3 09:05 CD XZ
答案 0 :(得分:0)
如果我理解您的问题,则希望合并两个数据框,并将datetime设置为datetime较高的datetime列。您可以按照以下步骤进行操作
创建虚拟数据进行测试
import pandas as pd
raw_data = {
"ids": range(1,6),
"datetime": ["7:55", "9:20", "8:11", "7:30", "11:25"],
"events": ["E11","E12", "E13", "E14", "E15"]
}
df1=pd.DataFrame(raw_data)
df1["datetime"]=pd.to_datetime(df1["datetime"], format="%H:%M")
display(df1)
raw_data = {
"ids": range(1,5),
"datetime": ["7:45", "9:30", "8:41", "7:20"],
"values": ["E21","E22", "E23", "E24"]
}
df2=pd.DataFrame(raw_data)
df2["datetime"]=pd.to_datetime(df2["datetime"], format="%H:%M")
display(df2)
合并
merged_df = pd.merge(df1, df2, on="ids", how="inner")
merged_df["datetime"] = merged_df["datetime_x"].where(merged_df["datetime_x"]>=merged_df["datetime_y"],
merged_df["datetime_y"])
display(merged_df)
merged_df.set_index("ids").reindex(columns=["datetime", "values", "events"])