我有以下数据帧
df
period remaining_et_months property_id beds
0 0 0 329 1
1 1 0 329 1
2 2 1 329 1
3 3 2 329 1
4 3 2 329 1
5 4 3 329 1
6 4 3 329 1
7 4 3 329 1
8 5 4 329 1
9 5 4 329 1
10 5 4 329 1
11 5 4 329 1
我需要对这个数据帧进行分组,并应用一个函数,以便能够计算一个名为
dist_period
:
g = df.groupby(['property_id', 'beds', 'period'])
g.apply(some_function)
我需要分组
property_id
,
beds
period
,因为数据帧比本例中的数据帧大。
我不清楚如何做到这一点,但我想要的是专栏
dist_周期
period remaining_et_months dist_period
0 0 0 0
1 1 0 1
2 2 1 1
3 3 2 1
4 3 2 2
5 4 3 1
6 4 3 2
7 4 3 3
8 5 4 1
9 5 4 2
10 5 4 3
11 5 4 4
注意,当组只有一个元素时
是
period - remaining_et_months