简体   繁体   中英

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. 在此处输入图片说明

Ideally I would like a graph similar to this where the LetterGrade would be language and the legend would refer to the level variable. 在此处输入图片说明

Using :

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM