samedi 30 mai 2020

To find mean using rolling function only if window has more than x data available

I have a dataframe df having columns like date, company name, price_standalone, price_consolidated, etc.

I want to find the mean of price column for the past 10 years with one condition.

If price_consolidated has data(i.e. it is not NaN), for the past 10 years then price_consolidated column's data to be used else price_standalone's data to be used.

df["Price mean 10 years"] = df.groupby('Company Name')["price_consolidated"].shift().rolling(min_periods=1, window=3650).mean()

This is how I calculated the mean. Can someone help me with the condition part of the code? Also if possible another column next to it stating whether price_consolidated is used of price_standalone is used. Thx.

Aucun commentaire:

Enregistrer un commentaire