Skip to content
Advertisement

Looping through a second column using a probability input

I have a similar question to one I posed here, but subtly different as it includes an extra step to the process involving a probability:

Using a Python pandas dataframe column as input to a loop through another column

I’ve got two pandas dataframes: one has these variables

JavaScript

Another is a table with these variables:

JavaScript

Essentially I need to use the Count x in the first dataframe to loop through the 2nd dataframe x times, but only pull a value from the 2nd dataframe y percent of the times (using random number generation) – and then create a new column in the first dataframe that represents the sum of the values in the loop.

So – just to demonstrate – in that first column, we’d loop through the 2nd table 8 times, but only pull a random value from that table 25% of the time – so we might get output of:

0 100 0 0 25 0 0 0

…which sums to 125 – so we our added column to the first table looks like

JavaScript

….and so on. Thanks in advance.

Advertisement

Answer

We’ll use numpy binomial and pandas sample to get this done.

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