You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas多列分组报错:如何按周分组显示销量最高的口味?

Pandas多列分组报错及需求实现

问题描述

自学Python/Pandas时,尝试按多列分组但触发错误,单独按单列分组可正常运行。

错误代码

max_by_week = ice_cream_sales_df.groupby('week','flavor').agg({"units sold":max})

错误信息

ValueError: No axis named flavor for object type DataFrame.

需求

按周分组,列出每周的最高销量,同时显示该销量对应的冰淇淋口味。

解决方法

  • 问题根源:groupby方法传入多列时,必须将列名放在列表中。你当前把'week'和'flavor'作为两个独立参数传入,第二个参数会被Pandas识别为axis参数,从而导致报错。
  • 修正方案1:分组后筛选最大值
    # 先按周和口味分组,计算每组销量最大值
    grouped = ice_cream_sales_df.groupby(['week', 'flavor'])['units sold'].max().reset_index()
    # 按周筛选出销量最高的记录
    max_by_week = grouped.loc[grouped.groupby('week')['units sold'].idxmax()]
    
  • 修正方案2:排序后去重(更简洁)
    max_by_week = ice_cream_sales_df.sort_values(['week', 'units sold'], ascending=[True, False])\
                                    .drop_duplicates('week')\
                                    .reset_index(drop=True)
    
    两种方案都能得到每周销量最高的完整记录,包含对应的口味。

此前可正常运行的代码及输出

代码

print(ice_cream_sales_df.head()); print(' ')

sorted_by_flavor = ice_cream_sales_df.sort_values('flavor')
print(sorted_by_flavor); print(' ')

sum_by_flavor = ice_cream_sales_df.groupby('flavor').agg({"units sold":sum})
print(sum_by_flavor); print(' ')

max_by_week = ice_cream_sales_df.groupby('week').agg({"units sold":max})
print(max_by_week);print(' ')

输出

week  units sold      flavor
0     1           6   chocolate
1     1          15       lemon
2     1          12  strawberry
3     1           6     vanilla
4     2          16   chocolate
 
     week  units sold     flavor
0       1           6  chocolate
124    32           2  chocolate
176    45          10  chocolate
36     10           7  chocolate
76     20          11  chocolate
..    ...         ...        ...
95     24           2    vanilla
91     23           5    vanilla
87     22           6    vanilla
79     20           8    vanilla
207    52          18    vanilla

[208 rows x 3 columns]
 
            units sold
flavor                
chocolate          460
lemon              713
strawberry         399
vanilla            527
 
      units sold
week            
1             15
2             18
3              9
4             20
5             20
6             11
7             13
8             18
9             12
10            18
11            18
12            16
13            15
14            14
15            20
16            18
17            16
18            20
19            18
20            13
21            18
22            18
23            15
24            19
25            16
26            11
27            14
28            13
29            18
30            20
31            16
32            17
33            16
34            19
35            18
36            18
37            16
38            11
39            15
40            20
41            14
42            17
43            17
44            20
45            15
46            19
47            18
48            15
49            17
50             9
51            20
52            18

内容的提问来源于stack exchange,提问作者Zaltark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 10:07:24