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:
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()