最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - In pandas, a groupby followed by boxplot gives KeyError: "None of [Index(['A', 1], dtype=&

programmeradmin2浏览0评论

This very simple script gives error 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()

So my dataframe is:

  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

and I would expect a plot with four boxplot showing the values V, the labels of boxplots should be A/1, A/2, B/1, B/2.

I had a look at How To Solve KeyError: u"None of [Index([..], dtype='object')] are in the [columns]" but I was not able to fix my error, AI tools are not helping me either.

What am I not understanding?

This very simple script gives error 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()

So my dataframe is:

  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

and I would expect a plot with four boxplot showing the values V, the labels of boxplots should be A/1, A/2, B/1, B/2.

I had a look at How To Solve KeyError: u"None of [Index([..], dtype='object')] are in the [columns]" but I was not able to fix my error, AI tools are not helping me either.

What am I not understanding?

Share Improve this question asked Feb 10 at 10:52 Alessandro JacopsonAlessandro Jacopson 18.7k16 gold badges104 silver badges156 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 3

You can use boxplot for the grouping as well

df.boxplot(column='V', by=['L1', 'L2'])

If you want to use groupby anyway, probaly you should create a single index for grouping, e.g.,

df.groupby(df['L1']+',' +df['L2'].astype(str)).boxplot(column = 'V',figsize = (8,8))

发布评论

评论列表(0)

  1. 暂无评论