Python 在装箱列上按分组不正确

Python 在装箱列上按分组不正确,python,python-3.x,pandas,Python,Python 3.x,Pandas,我使用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(['bi

我使用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列分组,但它给了我不正确的输出-实际上“35 inf”bin只有一个父帐户,但它显示的不止这些,我的代码中是否有错误?

没有提供示例数据。测试边缘情况似乎都很好。我用的是熊猫1.1

df = pd.DataFrame({"Number of Locations":[32,33,34,35,36,np.inf,np.nan,34.0001]})
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)
print(df.to_string())
输出

   Number of Locations   binned
0              32.0000    21-34
1              33.0000    21-34
2              34.0000    21-34
3              35.0000  35 -Inf
4              36.0000  35 -Inf
5                  inf  35 -Inf
6                  NaN      NaN
7              34.0001  35 -Inf