Skip to content
Advertisement

Pandas compare and sum values between two DataFrame with different size

Suppose I have two Dataframes with different sizes:

JavaScript

to which I have:

JavaScript

and:

JavaScript

Now I want to add a third column to df1 say total_volume, where it is the summation of the volume that lie between individual row of xlow and xup of df1. I can do this using:

JavaScript

we can check the value of say the second row as:

JavaScript

In reality, my df1 can have up to hundreds of thousands of rows, so it can take up to tens of minutes to complete. Is there a more vectorize/pythonic way of doing this. I tried pandas merge and join but were not successful, most likely because I am still a novice.

Thank you for all your help!

Advertisement

Answer

If bins are not overlapping is possible use cut with aggregate sum and then add to df1 by DataFrame.join:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement