I am trying to plot a line graph comparing the Murder Rates of particular States through the years 1960-1962 using Pandas in a Jupyter Notebook.
A little context about where I am now, and how I arrived here:
I'm using a crime csv file, which looks like this:
I'm only interested in 3 columns for the time being: State, Year, and Murder Rate. Specifically I was interested in only 5 states - Alaska, Michigan, Minnesota, Maine, Wisconsin.
So to produce the desired table, I did this (only showing top 5 row entries):
al_mi_mn_me_wi = crimes[(crimes['State'] == 'Alaska') | (crimes['State'] =='Michigan') | (crimes['State'] =='Minnesota') | (crimes['State'] =='Maine') | (crimes['State'] =='Wisconsin')]
control_df = al_mi_mn_me_wi[['State', 'Year', 'Murder Rate']]
From here I used the pivot function
df = control_1960_to_1962.pivot(index = 'Year', columns = 'State',values= 'Murder Rate' )
And this is where I get stuck. I received KeyError when doing (KeyError was Year):
df.plot(x='Year', y='Murder Rate', kind='line')
and when attempting just
df.plot()
I get this wonky graph.
How do I get my desired graph?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…