Skip to content
Advertisement

Using Python Pandas to bin data in one df according to bins defined in a second df

I am attempting to bin data in one dataframe according to bins defined in a second dataframe. I am thinking that some combination of pd.bin and pd.merge might get me there?

This is basically the form each dataframe is currently in:

JavaScript

df:

JavaScript

And this is the table with the bins, df2:

JavaScript

I would like to match the bin, and find the appropriate result in df2 using the cut_min and cut_max that encompasses the perc value in df. So, I would like the resulting table to look like this:

JavaScript

I originally wrote this in a SQL query which accomplished the task quite simply with a join:

JavaScript

If anyone knows a good way to do this using Pandas, it would be greatly appreciated! (And this is actually the first time I haven’t been able to find a solution just searching on stackoverflow, so my apologies if any of the above wasn’t explained well enough!)

Advertisement

Answer

First merge df and df2 on the bin column, and then select the rows where cut_min <= perc < cut_max:

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