Home > Enterprise >  pandas GroupBy plotting two lines for each group on one plot
pandas GroupBy plotting two lines for each group on one plot

Time:12-21

I've been struggling to plot the results of the GroupBy on three columns. I have the data on the different absences (AbsenceType) of employees (Employee) over 3 years (MonthYear). I would like to plot in one plot, how many absences of a particular type an employee had in each month-year. I have only two employees in the example, but there are more in the data as well as more month-year values.

Create data

data = {'Employee': ['ID1', 'ID1','ID1','ID1','ID1','ID1','ID1', 'ID1', 'ID1', 'ID2','ID2','ID2','ID2','ID2', 'ID2'],
'MonthYear': ['201708', '201601','201601','201708','201710','201801','201801', '201601', '201601', '201705', '201705', '201705', '201810', '201811', '201705'],
'AbsenceType': ['0210', '0210','0250','0215','0217','0260','0210', '0210', '0210', '0260', '0250', '0215', '0217', '0215', '0250']}

columns = ['Employee','MonthYear','AbsenceType']

df = pd.DataFrame(data, columns=columns)

Then I map each of the codes of the AbsenceType into two categories: Sick or Injury.

df['SickOrInjury'] =df['AbsenceType'].replace({'0210':'Sick', '0215':'Sick', '0217':'Sick', '0250':'Injury', '0260':'Injury'})

What I want to achieve is the following groupby:

test = df.groupby(['Employee', 'MonthYear', 'SickOrInjury'])['SickOrInjury'].count()

But, when I try to plot it, it does not fully show what I want. So far I managed to get to the stage:

df.groupby(['Employee', 'MonthYear', 'SickOrInjury'])['SickOrInjury'].count().unstack('SickOrInjury', fill_value=0).plot()
plt.show()

enter image description here

  • Related