最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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))

Post a comment

comment list (0)

  1. No comments so far