gpt4 book ai didi

python - Pandas 组内的动态子分组

转载 作者:行者123 更新时间:2023-11-28 21:41:36 24 4
gpt4 key购买 nike

有没有更简单/更正确的分配动态组的方法?让我们说我们有以下 df:

group    days(int, >0)
A 1
B 12
A 14
A 16
A 19
B 23
C 92
C 12

我想根据以下规则分配子组:

if days >20 then subgroup = 4
if days <= 20 then subgroup = 3
if days <= 10 then subgroup = 2
if days == 0 then subgroup = 1

这是我现在的做法:

df['subgroup'] = 4
df.loc[df['days'] >20,'subgroup'] = 4
df.loc[df['days'] <=20,'subgroup'] = 3
df.loc[df['days'] <=10,'subgroup'] = 2
df.loc[df['days'] ==0,'subgroup'] = 1
df = df.reset_index()
df['dynamic_subgroup'] = df.groupby(['group'])['subgroup'].rank(method='dense')

结果表是这个:

group    days(int, >0)     dynamic_subgroup
A 1 1
B 12 1
A 14 2
A 16 3
A 19 4
B 23 2
C 92 2
C 12 1

我想知道是否有更简单/更好的方法可以在 Pandas 中实现相同的结果?通常,对代码的任何更正表示赞赏。

最佳答案

您可以使用 cut用于装箱:

bins = [-1, 0, 10, 20, np.inf]
labels=[1,2,3,4]
df['subgroup'] = pd.cut(df['days'], bins=bins, labels=labels)
print (df)
group days subgroup
0 A 1 2
1 B 12 3
2 A 14 3
3 A 16 3
4 A 19 3
5 B 23 4
6 C 92 4
7 C 12 3

关于python - Pandas 组内的动态子分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44669507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com