4.3 Exercise 1: Students in Majors

Analyse the data from the file spsClass.xls to determine the number of students in each of the major departments.

Plot this data as a bar chart as shown below.

4.3.1 A Solution

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel('spsClass.xls') 

df_grp_result = df.groupby(by='Major')['Major'].count()
ax = df_grp_result.sort_values().plot(kind='bar',
                                      color=['red', 'green', 'blue', 'yellow'],
                                      alpha=.5,
                                      rot=0)
_= ax.set_ylabel('Number of Students')
plt.show()