Marisa
Marisa

Reputation: 1183

How to group columns by label in a histogram using a panda DataFrame?

I have a panda dataframe called language consisting of two columns:

    lang          level
0      english         2
1      spanish         2 
2      spanish         1
3      english         1
4      english         3
5      spanish         2
6      spanish         1
7      spanish         3

I would like to represent it in a histogram group by the categorical value language in such a way that in the same plot I have 2 groups -one for each language- with as many bar as many labels I have in the level column (3 in this case).

So far I have tried the following by previously categorizaing lang getting a label of 1 to english and of 2 to spanish:

language.hist(by=language['lang'])

With what I obtained the following graph which is not what I want.enter image description here

Ideally I would like a graph similar to this where the LetterGrade would be language and the legend would refer to the level variable.enter image description here

Upvotes: 0

Views: 895

Answers (1)

BENY
BENY

Reputation: 323226

Using :

pd.crosstab(df.lang,df.level).plot(kind='bar')

enter image description here

Upvotes: 2

Related Questions