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

Performing a calculation on a list of variables in python pandas

I have a calculation to create a new column in a dataframe in pandas that looks looks like this:

day_amount = 3

offset = (day_amount -1)//2
days = str(day_amount)
 
df[days+'day-min'] = df[variable].shift(-offset).rolling(day_amount,min_periods=(int(0.75*day_amount))).min()

It takes an amount of days [day_amount] and produces an offset and then creates a single new column with a rolling day minimum of [variable] column based on the number of [day amount] set previously.

I’d like to loop through a list of [day_amount]s instead of the single variable (3) in the above example, for instance:

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

day_amount = [3, 5, 8, 11]

for i in day_amount:
    offset = (i -1)//2
    days = str(i)

I don’t think the above is correct, but hopefully is clear what I’m looking to do. I’d like the end result to be multiple new columns created in the dataframe that have results from the same calculation, but using each value in the [day_amount] list. So one column for the calc using 3 days, one for 5, one for 8, and so on.

Thanks

>Solution :

It seems to me your code looks fine if you change the variable names.

day_amounts = [3, 5, 8, 11]

for day_amount in day_amounts:
    offset = (day_amount -1)//2
    days = str(day_amount)
    df[days+'day-min'] = df[variable].shift(-offset).rolling(day_amount,min_periods=(int(0.75*day_amount))).min()
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