[英]Plotting Pandas DataFrame from Pivot table
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. 我正在尝试绘制折线图,比较在Jupyter笔记本中使用熊猫的1960-1962年特定国家的谋杀率。
A little context about where I am now, and how I arrived here: 关于我现在的位置以及如何到达这里的一些背景信息:
I'm using a crime csv file, which looks like this: 我正在使用犯罪csv文件,该文件如下所示:
I'm only interested in 3 columns for the time being: State, Year, and Murder Rate. 我目前只对3栏感兴趣:州,年份和谋杀率。 Specifically I was interested in only 5 states - Alaska, Michigan, Minnesota, Maine, Wisconsin. 具体来说,我只对5个州感兴趣-阿拉斯加,密歇根州,明尼苏达州,缅因州,威斯康星州。
So to produce the desired table, I did this (only showing top 5 row entries): 因此,为了生成所需的表,我这样做了(仅显示前5行条目):
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): 我在执行操作时收到KeyError(KeyError是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? 如何获得所需的图形?
import numpy as np
import pandas as pd
control_1960_to_1962 = pd.DataFrame({
'State': np.repeat(['Alaska', 'Maine', 'Michigan', 'Minnesota', 'Wisconsin'], 3),
'Year': [1960, 1961, 1962]*5,
'Murder Rate': [10.2, 11.5, 4.5, 1.7, 1.6, 1.4, 4.5, 4.1, 3.4, 1.2, 1.0, .9, 1.3, 1.6, .9]
})
df = control_1960_to_1962.pivot(index='Year', columns='State', values='Murder Rate')
You can tell Pandas (and through it the matplotlib
package that actually does the plotting) what xticks you want explicitly: 您可以明确地告诉Pandas(并通过它实际执行绘图的matplotlib
包)告诉您想要什么xticks:
ax = df.plot(xticks=df.index)
ylab = ax.set_ylabel('Murder Rate')
Output: 输出:
ax
is a matplotlib.axes.Axes
object , and there are many, many customizations you can make to your plot through it. ax
是matplotlib.axes.Axes
对象 ,您可以通过它对绘图进行很多很多定制。
Here's how to plot with the States
on the x axis: 以下是如何与剧情States
在x轴:
ax = df.T.plot(kind='bar')
ylab = ax.set_ylabel('Murder Rate')
Output: 输出:
try this you can explore more 试试这个你可以探索更多
pip install pivottablejs
import pandas as pd
import numpy as np
from pivottablejs import pivot_ui
df = pd.DataFrame({
'State': np.repeat(['Alaska', 'Maine', 'Michigan', 'Minnesota','Wisconsin'], 3),
'Year': [1960, 1961, 1962]*5,
'Murder Rate': [10.2, 11.5, 4.5, 1.7, 1.6, 1.4, 4.5, 4.1, 3.4, 1.2, 1.0, .9, 1.3, 1.6, .9]})
pivot_ui(df) pivot_ui(DF)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.