This might be a tough question though I thought it was easy. So the problem is that I want to calculate the mean in N interval.
To simplify my data, the original data is like
Group | Animal |
---|---|
1 | 6 |
3 | 5 |
2 | 4 |
4 | nan |
5 | 4 |
4 | nan |
And I would like to do average with each 3 steps,which means that the expecting df[Group]
shows in three rows
Group
2.5 -->(1 4)/2
4 --> (3 5)/2
3 --> (2 4)/2
And the other problem is encountering NaN value, so if it has NaN value, we just calculate the value without NaN (and of course not showing NaN)
Thus, the ideal result in df[Animal]
will be
Animal
6 --> (only 6, NaN cannot be calculated)
4.5 --> (5 4)/2
4 --> (4, NaN cannot be calculated)
shows in three rows Any ideas to solve it, I appreciate it!
CodePudding user response:
You can try with %
: remainder
out = df.groupby(df.index%3).mean()
Out[127]:
Group Animal
0 2.5 6.0
1 4.0 4.5
2 3.0 4.0