1

I have made a pie chart using an excel sheet but it is coming out incomplete. I am not sure of the reason. Here is the code:

import matlotplib.pyplot as plt
import pandas as pd
import numpy as np
Employee=pd.read_excel("C:\\Users\\Jon\\Desktop\\data science\\Employee.xlsx")
Employee

colors = ["#1f77b4", "#ff7f0e"]
group_by_departments=Employee.groupby("Department").count().reset_index()
sizes = group_by_departments['Gender']
labels = group_by_departments['Department']
plt.pie(sizes, labels=labels, colors = colors,autopct='%.2f %%')
plt.show()

enter image description here

enter image description here

Jon
  • 43
  • 5
  • A sample of `Employee` would help. What do you mean "it is coming out incomplete"? – Henry Ecker Nov 06 '21 at 04:31
  • It is missing a legend which should show 'Gender'. I cannot tell whether it is showing the number of employees in each department or just showing one gender. – Jon Nov 06 '21 at 04:54

1 Answers1

1

You can use .size() to get the count for each group. You'll need to group by Department and Gender simultaneously to obtain the individual counts of all the subgroups.

Here is some example code:

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

N = 100
Employee = pd.DataFrame({'Gender': np.random.choice(['Male', 'Female'], N),
                         'Department': np.random.choice(['IT', 'Sales', 'HR', 'Finance'], N),
                         'Age': np.random.randint(20, 65, N),
                         'Salary': np.random.randint(20, 100, N) * 1000})
colors = ["turquoise", "tomato"]
group_by_departments_and_gender = Employee.groupby(["Department", "Gender"]).size().reset_index(name='Counts')
sizes = group_by_departments_and_gender['Counts']
labels = [f'{dept}\n {gender}' for dept, gender in group_by_departments_and_gender[['Department', 'Gender']].values]
plt.pie(sizes, labels=labels, colors=colors, autopct='%.2f %%')
plt.tight_layout()
plt.show()

two-leveled pie chart

PS: You could assign a color per gender via:

colors = ["magenta" if gender=="Male" else "deepskyblue" for gender in group_by_departments_and_gender["Gender"]]

This especially helps in case one of the genders wouldn't be present in one of the departments.

JohanC
  • 71,591
  • 8
  • 33
  • 66