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

Pandas中groupby后调用boxplot出现KeyError的问题求助

Pandas中groupby后调用boxplot出现KeyError的问题求助

这个非常简单的脚本运行时抛出了如下错误:

KeyError: "None of [Index(['A', 1], dtype='object')] are in the [index]"

脚本代码如下:

import pandas as pd
import matplotlib.pyplot as plt
L1 = ['A','A','A','A','B','B','B','B']
L2 = [1,1,2,2,1,1,2,2]
V = [9.8,9.9,10,10.1,19.8,19.9,20,20.1]
df = pd.DataFrame({'L1':L1,'L2':L2,'V':V})

print(df)

df.groupby(['L1','L2']).boxplot(column='V')
plt.show()

我的DataFrame内容是:

L1  L2     V
0  A   1   9.8
1  A   1   9.9
2  A   2  10.0
3  A   2  10.1
4  B   1  19.8
5  B   1  19.9
6  B   2  20.0
7  B   2  20.1

我期望生成的图表包含四个箱线图来展示V的数值,箱线图的标签应该是A/1、A/2、B/1、B/2。

我已经查阅过相关的问题解决方案,但还是没能修复这个错误,AI工具也没帮上忙。

我到底哪里理解错了呢?

备注:内容来源于stack exchange,提问作者Alessandro Jacopson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 09:49:30