I have two pandas dataframes.
The first dataframe contains data recorded in millisecond
intervals.
Time Price
1643669589971 145.77
1643669589977 145.78
1643669589984 145.79
The second dataframe contains data recorded in 1 minute
intervals.
Time Price
1643669589971 145.77
1643669649971 146.33
1643669709971 147.02
I would like to map the equivalent last_minute
data in the millisecond data like this.
Time Price last_minute_time last_minute_price
1643669589971 145.77 1643669589971 145.77
1643669589977 145.78 1643669589971 145.77
1643669589984 145.79 1643669589971 145.77
Is there any easier way to do this in pandas?
Thanks
CodePudding user response:
Use merge_asof
with DataFrame.add_prefix
:
df = pd.merge_asof(df1,
df2.add_prefix('last_minute_'),
left_on='Time',
right_on='last_minute_Time')
print (df)
Time Price last_minute_Time last_minute_Price
0 1643669589971 145.77 1643669589971 145.77
1 1643669589977 145.78 1643669589971 145.77
2 1643669589984 145.79 1643669589971 145.77