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

      <bdo id='ceKN4'></bdo><ul id='ceKN4'></ul>
      <legend id='ceKN4'><style id='ceKN4'><dir id='ceKN4'><q id='ceKN4'></q></dir></style></legend>

    1. <small id='ceKN4'></small><noframes id='ceKN4'>

    2. <tfoot id='ceKN4'></tfoot>

      1. Pandas 数据框中的 MultiIndex Group By

        MultiIndex Group By in Pandas Data Frame(Pandas 数据框中的 MultiIndex Group By)
        <tfoot id='x0Gt6'></tfoot>

          <bdo id='x0Gt6'></bdo><ul id='x0Gt6'></ul>
                <tbody id='x0Gt6'></tbody>

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

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

                  本文介绍了Pandas 数据框中的 MultiIndex Group By的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数据集,其中包含按年份划分的国家和经济指标统计数据,组织方式如下:

                  I have a data set that contains countries and statistics on economic indicators by year, organized like so:

                  Country  Metric           2011   2012   2013  2014
                    USA     GDP               7      4     0      2
                    USA     Pop.              2      3     0      3
                    GB      GDP               8      7     0      7
                    GB      Pop.              2      6     0      0
                    FR      GDP               5      0     0      1
                    FR      Pop.              1      1     0      5
                  

                  如何在 pandas 中使用 MultiIndex 来创建仅按年份显示每个国家/地区的 GDP 的数据框?

                  How can I use MultiIndex in pandas to create a data frame that only shows GDP by Year for each country?

                  我试过了:

                  df = data.groupby(['Country', 'Metric'])
                  

                  但它不能正常工作.

                  推荐答案

                  在这种情况下,您实际上并不需要 groupby.你也没有MultiIndex.你可以做一个这样的:

                  In this case, you don't actually need a groupby. You also don't have a MultiIndex. You can make one like this:

                  import pandas
                  from io import StringIO
                  
                  datastring = StringIO("""
                  Country  Metric           2011   2012   2013  2014
                  USA     GDP               7      4     0      2
                  USA     Pop.              2      3     0      3
                  GB      GDP               8      7     0      7
                  GB      Pop.              2      6     0      0
                  FR      GDP               5      0     0      1
                  FR      Pop.              1      1     0      5
                  """)
                  data = pandas.read_table(datastring, sep='ss+')
                  data.set_index(['Country', 'Metric'], inplace=True)
                  

                  那么 data 是这样的:

                                  2011  2012  2013  2014
                  Country Metric                        
                  USA     GDP        7     4     0     2
                          Pop.       2     3     0     3
                  GB      GDP        8     7     0     7
                          Pop.       2     6     0     0
                  FR      GDP        5     0     0     1
                          Pop.       1     1     0     5
                  

                  现在要获取 GDP,您可以通过 xs 方法获取数据框的横截面:

                  Now to get the GDPs, you can take a cross-section of the dataframe via the xs method:

                  data.xs('GDP', level='Metric')
                  
                           2011  2012  2013  2014
                  Country                        
                  USA         7     4     0     2
                  GB          8     7     0     7
                  FR          5     0     0     1
                  

                  这很容易,因为您的数据已经旋转/取消堆叠.如果他们不是并且看起来像这样:

                  It's so easy because your data are already pivoted/unstacked. IF they weren't and looked like this:

                  data.columns.names = ['Year']
                  data = data.stack()
                  data
                  
                  Country  Metric  Year
                  USA      GDP     2011    7
                                   2012    4
                                   2013    0
                                   2014    2
                           Pop.    2011    2
                                   2012    3
                                   2013    0
                                   2014    3
                  GB       GDP     2011    8
                                   2012    7
                                   2013    0
                                   2014    7
                           Pop.    2011    2
                                   2012    6
                                   2013    0
                                   2014    0
                  FR       GDP     2011    5
                                   2012    0
                                   2013    0
                                   2014    1
                           Pop.    2011    1
                                   2012    1
                                   2013    0
                                   2014    5
                  

                  然后您可以使用 groupby 告诉您有关整个世界的一些信息:

                  You could then use groupby to tell you something about the world as a whole:

                  data.groupby(level=['Metric', 'Year']).sum()
                  Metric  Year
                  GDP     2011    20
                          2012    11
                          2013     0
                          2014    10
                  Pop.    2011     5
                          2012    10
                          2013     0
                          2014     8
                  

                  或者得到真正的幻想:

                  data.groupby(level=['Metric', 'Year']).sum().unstack(level='Metric')
                  Metric  GDP  Pop.
                  Year             
                  2011     20     5
                  2012     11    10
                  2013      0     0
                  2014     10     8
                  

                  这篇关于Pandas 数据框中的 MultiIndex Group By的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                  <tfoot id='X0LBJ'></tfoot>
                      <bdo id='X0LBJ'></bdo><ul id='X0LBJ'></ul>
                        <tbody id='X0LBJ'></tbody>

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

                          <legend id='X0LBJ'><style id='X0LBJ'><dir id='X0LBJ'><q id='X0LBJ'></q></dir></style></legend>

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