我正在使用 pandas cut 来计算一个新的 bins 列,如下所示:
bins = [1, 10, 20, 34, np.Inf]
labels = ['1-10', '11-20', '21-34', '35 -Inf']
df['binned'] = pd.cut(df['Number of Locations'], bins=bins, labels=labels, include_lowest=True)
这为我提供了合并值的新列,
然后我尝试下面的代码:
df.groupby(['binned', 'Parent_Account'])['has_desired_product'].apply(sum).reset_index()
这应该给我按新的 binned 列分组,但它给了我不正确的输出 - 实际上只有一个 Parent_Account 用于'35-inf' bin 但它显示的不止于此,我的代码某处有错误吗?
互换的青春
相关分类