1

I want to insert a dataframe into another dataframe with Time key with ID appear to follow the Time. How to do it?

my data looks like this:

df1
ID  Time
ID1 10:40
ID1 11:00
ID2 11:10
ID2 11:30

df2
Time    X   Y
10:40   45  50
10:50   55  55
11:00   65  60
11:10   75  65
11:20   85  70
11:30   95  75

my expected result looks like this:

ID  Time    X   Y
ID1 10:40   45  50
ID1 10:50   55  55
ID1 11:00   65  60
ID2 11:10   75  65
ID2 11:20   85  70
ID2 11:30   95  75

thank you for your help

Arief Hidayat
  • 937
  • 1
  • 8
  • 19

1 Answers1

1

Use merge_asof with datetimes columns, last use Series.dt.strftime for convert it to HH:MM strings:

df1['Time'] = pd.to_datetime(df1['Time'])
df2['Time'] = pd.to_datetime(df2['Time'])

df = pd.merge_asof(df2, df1, on='Time').assign(Time = lambda x: x['Time'].dt.strftime('%H:%M'))
print (df)
    Time   X   Y   ID
0  10:40  45  50  ID1
1  10:50  55  55  ID1
2  11:00  65  60  ID1
3  11:10  75  65  ID2
4  11:20  85  70  ID2
5  11:30  95  75  ID2
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252