Skip to content
Advertisement

How would I find the quarterly averages of these monthly figures?

My dataset is similar to the below:

JavaScript

enter image description here

How can I add columns to this which show the quarterly figure, which is an average of the preceding three months? Eg, suppose we started at adding a column after ‘Dec-21’ called Q4 2021 which took the average of the columns called ‘Oct-21’, ‘Nov-21’ and ‘Dec-21’.

Will I need to create a function which takes the preceding three values and returns an average, and then concatenate this to my dataframe? It does not have to be directly after each period, eg I am also happy to add all of the Quarterly averages right at the end.

Advertisement

Answer

JavaScript

df_quarter is your final answer which you can merge back to original df

Output:

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