Wednesday, 11 September 2013

Mean in group of columns

Mean in group of columns

I have a data frame with monthly values and I want to obtain quaterly
values as an average of 3 months group My data are like that (only first 9
months for the example)
month 01 02 03 04 05 06 07 08 09 \
year
2000 90.26 90.95 91.04 90.87 90.78 91.13 90.87 90.95 91.30
2000 87.89 89.68 90.10 90.27 90.53 90.87 89.93 91.30 91.98
2000 74.17 74.98 74.74 73.97 74.07 74.26 74.71 76.93 78.67
2000 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2000 86.74 85.48 87.45 88.31 88.71 88.23 88.08 87.76 88.94
And I want obtain Q1 as the mean of months 01,02,03. i can do:
df['Q1']=(df['01']+df['02']+df['03'])/3
But I will have problems with the Nan.
Can I calculate means in groups of three months?

No comments:

Post a Comment