Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging data in pandas

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading