想象一個如下所示的資料集:
result country start end
5 A 2/14/2022 2/21/2022
10 A 2/21/2022 2/28/2022
30 B 2/28/2022 3/7/2022
50 C 1/3/2022 1/10/2022
60 C 1/10/2022 1/17/2022
70 D 1/17/2022 1/24/2022
40 E 1/24/2022 1/31/2022
20 E 1/31/2022 2/7/2022
30 A 2/7/2022 2/14/2022
20 B 2/14/2022 2/21/2022
預期產出
我需要進行分組(國家、開始和結束),結果列應該將現有值與上述值相加,并且需要填充平均列。例如:
groupby country, start, and end with result and average column is nothing but 5, 5 10/2, 10 30/2, 30 50/2,50 60/2
result average
5 5 eg: (5)
10 7.5 (5 10/2) #resultcol of existingvalue abovevalue divided by 2 = average
30 20 (10 30/2)
50 40 (30 50/2)
60 55 (50 60/2)
70 65 ...
40 55 ...
20 30 ...
30 25 ...
20 25 ...
uj5u.com熱心網友回復:
嘗試按 country 和 date 分組的解決方案,但是如果子集中沒有足夠的資料(即大于 2)可能會引發錯誤:
df_data['average'] = df_data.groupby(['country', 'date'])['result'].rolling(2, min_periods=1).mean().reset_index(0, drop=True)
如果您只想按國家/地區分組
df_data['average'] = df_data.groupby(['country'])['result'].rolling(2, min_periods=1).mean().reset_index(0, drop=True)
df_data
country date result average
0 A 2/14/2022 5 5.0
1 A 2/21/2022 10 7.5
2 B 2/28/2022 30 30.0
3 C 1/3/2022 50 50.0
4 C 1/10/2022 60 55.0
5 D 1/17/2022 70 70.0
6 E 1/24/2022 40 40.0
7 E 1/31/2022 20 30.0
8 A 2/7/2022 30 20.0
9 B 2/14/2022 20 25.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/431696.html
標籤:Python python-3.x 熊猫 数据框 平均的
上一篇:根據日期范圍選擇資料框中的行
