我想用groupby添加一组分组的行数。 示例:
name, amount
test, 1
test2, 5
test, 3
test3, 8
test2, 4
test, 1
应该给出:
name, amount, count
test, 5, 3
test2, 9, 2
test3, 8, 1
我当前的groupby语句:
df = (df.groupby('name')['amount'].sum().reset_index().sort_values(by='name', ascending=True))
...
df = df.groupby(pd.cut(df['name'],
bins=[0,1,2],
labels=['A', 'B']))['amount'].sum().reset_index()