简体   繁体   中英

How Plot a bar Graph in python from Pivot Table() result?

I've calculated total Employees in company based on their ' Gender ' and ' Educational Background '. Now I want to visualize it in the form of bar plots, But I've tried to do, still I'm unable to view it visually.

Code:

df1 = pd.pivot_table(comp2, index = ['Gender', 'EducationBackground'], aggfunc={'EducationBackground':'count'})

Result:

"""
                             EducationBackground
Gender  EducationBackground 
Female        Life Sciences                   30
                  Marketing                   15
                    Medical                   21
                      Other                    2
           Technical Degree                    7
  Male      Human Resources                    3
              Life Sciences                   48
                  Marketing                   14
                    Medical                   42
                      Other                    1
           Technical Degree                   11

Visualize:

sns.countplot(df1,hue='EducationBackground')

Error:

Could not interpret input 'EducationBackground'

sns.countplot already performs an aggregation (similar to what you are doing in pivot_table ), so you can probably obtain what you need by using sns.countplot directly on the comp2 dataframe, something like:

sns.countplot(x='EducationBackground', hue="Gender", data=comp2)

NB: you can obtain a similar result by using sns.barplot on df1 .

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