Reputation: 11
I have the data as
A=[0.1,0.3,0.5,0.7,0.9,1.1,1.3,1.5,1.7]
B=[3,4,6,8,2,10,2,3,4]
If A is my index and B is the value corresponding to A. I have to group the first three i.e [0.1,0.3,0.5]
and calculate the average in B i.e [3,4,6]
. similarly average of 2nd 3 data [8,2,10]
corresponding to [0.7,0.9,1.1]
and again of [2,3,4]
corresponding to [1.3,1.5,1.7]
and then prepare the table for this three average values. Final Data frame should be like
A=[1,2,3]
B=[average 1, average 2, average 3]
Upvotes: 1
Views: 32
Reputation: 862641
If need aggregate mean
by each 3 values use helper array by length of DataFrame with integer division by 3 with GroupBy.mean
:
A=[0.1,0.3,0.5,0.7,0.9,1.1,1.3,1.5,1.7]
B=[3,4,6,8,2,10,2,3,4]
df = pd.DataFrame({'col':B}, index=A)
print (df)
col
0.1 3
0.3 4
0.5 6
0.7 8
0.9 2
1.1 10
1.3 2
1.5 3
1.7 4
df = df.groupby(np.arange(len(df)) // 3).mean()
df.index +=1
print (df)
col
1 4.333333
2 6.666667
3 3.000000
Upvotes: 1