Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
74 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can do Floor division on index and then groupby and mean:

df.groupby(df.index//4).mean()

If index is not a range index do:

out = df.reset_index()
out.drop("index",1).groupby(out.index//4).mean()

Or,

df.groupby(np.arange(len(df)) // 4).mean()

    Gen1    Gen2    Gen3   Gen4
0  24.75  121.00   50.00  27.25
1  36.00   21.25   32.75  35.50
2  70.00   46.25   52.25  25.75
3  46.50   26.75  273.50  32.50
4  40.25   24.75   38.25  95.50

Check out this answer: Calculate average of every x rows in a table and create new table


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...