<i id='QgVzg'><tr id='QgVzg'><dt id='QgVzg'><q id='QgVzg'><span id='QgVzg'><b id='QgVzg'><form id='QgVzg'><ins id='QgVzg'></ins><ul id='QgVzg'></ul><sub id='QgVzg'></sub></form><legend id='QgVzg'></legend><bdo id='QgVzg'><pre id='QgVzg'><center id='QgVzg'></center></pre></bdo></b><th id='QgVzg'></th></span></q></dt></tr></i><div id='QgVzg'><tfoot id='QgVzg'></tfoot><dl id='QgVzg'><fieldset id='QgVzg'></fieldset></dl></div>
    <legend id='QgVzg'><style id='QgVzg'><dir id='QgVzg'><q id='QgVzg'></q></dir></style></legend>
  • <small id='QgVzg'></small><noframes id='QgVzg'>

    1. <tfoot id='QgVzg'></tfoot>
      • <bdo id='QgVzg'></bdo><ul id='QgVzg'></ul>

        从枢轴绘制 Pandas DataFrame

        时间:2023-10-19
          1. <i id='bqDk1'><tr id='bqDk1'><dt id='bqDk1'><q id='bqDk1'><span id='bqDk1'><b id='bqDk1'><form id='bqDk1'><ins id='bqDk1'></ins><ul id='bqDk1'></ul><sub id='bqDk1'></sub></form><legend id='bqDk1'></legend><bdo id='bqDk1'><pre id='bqDk1'><center id='bqDk1'></center></pre></bdo></b><th id='bqDk1'></th></span></q></dt></tr></i><div id='bqDk1'><tfoot id='bqDk1'></tfoot><dl id='bqDk1'><fieldset id='bqDk1'></fieldset></dl></div>
          2. <tfoot id='bqDk1'></tfoot>
            <legend id='bqDk1'><style id='bqDk1'><dir id='bqDk1'><q id='bqDk1'></q></dir></style></legend>

            <small id='bqDk1'></small><noframes id='bqDk1'>

              <tbody id='bqDk1'></tbody>

              <bdo id='bqDk1'></bdo><ul id='bqDk1'></ul>

                  本文介绍了从枢轴绘制 Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 Jupyter Notebook 中使用 Pandas 绘制一个比较特定州在 1960-1962 年间的谋杀率的折线图.

                  关于我现在在哪里以及我是如何到达这里的一些背景信息:

                  我正在使用犯罪 csv 文件,如下所示:

                  我目前只对 3 个栏目感兴趣:州、年份和谋杀率.具体来说,我只对 5 个州感兴趣——阿拉斯加、密歇根、明尼苏达、缅因、威斯康星.

                  为了生成所需的表格,我这样做了(仅显示前 5 行条目):

                  al_mi_mn_me_wi = 犯罪[(crimes['State'] == 'Alaska') |(犯罪['州'] =='密歇根')|(犯罪['州'] =='明尼苏达')|(犯罪['州'] =='缅因州')|(犯罪['州'] =='威斯康星州')]control_df = al_mi_mn_me_wi[['状态', '年份', '谋杀率']]

                  从这里我使用了 pivot 功能

                  df = control_1960_to_1962.pivot(index = 'Year', columns = 'State',values= 'Murder Rate' )

                  这就是我卡住的地方.我在做的时候收到了 KeyError(KeyError 是年份):

                  df.plot(x='Year', y='Murder Rate', kind='line')

                  当尝试时

                  df.plot()

                  我得到了这个不稳定的图表.

                  如何获得我想要的图表?

                  解决方案

                  给定一个长(整齐)格式的数据帧,pandas.DataFrame.pivot 用于转换为宽格式,即可以直接用 pandas.DataFrame.plot

                  绘制

                  python 3.8.11pandas 1.3.3matplotlib 3.4.3

                  将 numpy 导入为 np将熊猫导入为 pdcontrol_1960_to_1962 = pd.DataFrame({'州': np.repeat(['阿拉斯加', '缅因州', '密歇根州', '明尼苏达州', '威斯康星州'], 3),‘年份’:[1960, 1961, 1962]*5,谋杀率":[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')# 显示(df)阿拉斯加州缅因州密歇根州明尼苏达威斯康星州年1960 10.2 1.7 4.5 1.2 1.31961 11.5 1.6 4.1 1.0 1.61962 4.5 1.4 3.4 0.9 0.9

                  地块

                  您可以明确告诉 Pandas(并通过它实际执行绘图的 matplotlib 包)您想要的 xticks:

                  ax = df.plot(xticks=df.index, ylabel='谋杀率')

                  输出:

                  ax

                  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?

                  解决方案

                  Given a dataframe in a long (tidy) format, pandas.DataFrame.pivot is used to transform to a wide format, which can be plotted directly with pandas.DataFrame.plot

                  Tested in python 3.8.11, pandas 1.3.3, matplotlib 3.4.3

                  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')
                  
                  # display(df)
                  State  Alaska  Maine  Michigan  Minnesota  Wisconsin
                  Year                                                
                  1960     10.2    1.7       4.5        1.2        1.3
                  1961     11.5    1.6       4.1        1.0        1.6
                  1962      4.5    1.4       3.4        0.9        0.9
                  

                  The plots

                  You can tell Pandas (and through it the matplotlib package that actually does the plotting) what xticks you want explicitly:

                  ax = df.plot(xticks=df.index, 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.

                  Here's how to plot with the States on the x axis:

                  ax = df.T.plot(kind='bar', ylabel='Murder Rate')
                  

                  Output:

                  这篇关于从枢轴绘制 Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 (row,col,values) 的元组列表构造 pandas DataFrame 下一篇:Pandas:如何将行中的一列旋转为列

                  相关文章

                • <small id='okY8v'></small><noframes id='okY8v'>

                  <tfoot id='okY8v'></tfoot>

                      <legend id='okY8v'><style id='okY8v'><dir id='okY8v'><q id='okY8v'></q></dir></style></legend>
                      <i id='okY8v'><tr id='okY8v'><dt id='okY8v'><q id='okY8v'><span id='okY8v'><b id='okY8v'><form id='okY8v'><ins id='okY8v'></ins><ul id='okY8v'></ul><sub id='okY8v'></sub></form><legend id='okY8v'></legend><bdo id='okY8v'><pre id='okY8v'><center id='okY8v'></center></pre></bdo></b><th id='okY8v'></th></span></q></dt></tr></i><div id='okY8v'><tfoot id='okY8v'></tfoot><dl id='okY8v'><fieldset id='okY8v'></fieldset></dl></div>

                      • <bdo id='okY8v'></bdo><ul id='okY8v'></ul>