0

I want to make two trend lines in the plot. I wrote a code but it is not working as expected. Is there any other way to do this?

by_type = df.filter(["Total","Academic_Year","Institute_Type"]).groupby(["Institute_Type","Academic_Year"]).sum()
print(by_type)
by_type=df.pivot(index='Academic_Year', columns='Institute_Type', values='Total')
by_type.plot()

This is the result of the print command.

Institute_Type Academic_Year   Total
Federal        2012-2013       83302.0
               2013-2014       90770.0
               2014-2015       95230.0
               2015-2016       92828.0
               2016-2017       93274.0
               2017-2018       93424.0
               2018-2019       92442.0
               2019-2020       89632.0
Non-Federal    2012-2013      153084.0
               2013-2014      165788.0
               2014-2015      183580.0
               2015-2016      186290.0
               2016-2017      181236.0
               2017-2018      180298.0
               2018-2019      170436.0
               2019-2020      172996.0

but pivot command gives me an error

ValueError: Index contains duplicate entries, cannot reshape

What I want to do is I want to make two trend lines(Federal and Non-Federal) in the plot with two colors to indicate how the Total value varies for each academic year.

1 Answers1

0
import matplotlib.pyplot as plt
# Convert back to single dataframe instead of groups
df = by_type.apply(pd.DataFrame)
# Loop through, grouping by JUST Institute_Type
for Institute_Type, vals in df.groupby('Institute_Type'):
    vals.plot(x='Academic_Year', y='Total', kind='line',
              label=Institute_Type, ax=plt.gca())
Andrew Pye
  • 558
  • 3
  • 16