• <tfoot id='1tTub'></tfoot>

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

        • <bdo id='1tTub'></bdo><ul id='1tTub'></ul>
      1. <legend id='1tTub'><style id='1tTub'><dir id='1tTub'><q id='1tTub'></q></dir></style></legend>

      2. <small id='1tTub'></small><noframes id='1tTub'>

        如何在 pandas 中使用groupby根据另一列中的条件计算百分比/比例总数

        How to use groupby in pandas to calculate a percentage / proportion total based on a criteria in another column(如何在 pandas 中使用groupby根据另一列中的条件计算百分比/比例总数)

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

          • <tfoot id='G5aua'></tfoot>

                  <tbody id='G5aua'></tbody>

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

                  本文介绍了如何在 pandas 中使用groupby根据另一列中的条件计算百分比/比例总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试研究如何使用 pandas 中的 groupby 函数在给定的是/否标准下计算出每年值的比例.

                  I'm trying to work out how to use the groupby function in pandas to work out the proportions of values per year with a given Yes/No criteria.

                  例如,我有一个名为 names 的数据框:

                  For example, I have a dataframe called names:

                    Name  Number  Year   Sex Criteria
                  0  name1     789  1998  Male      N
                  1  name1     688  1999  Male      N
                  2  name1     639  2000  Male      N
                  3  name2     551  1998  Male      Y
                  4  name2     499  1999  Male      Y
                  

                  我可以使用

                  namesgrouped = names.groupby(["Sex", "Year", "Criteria"]).sum()
                  

                  得到:

                                     Number
                  Sex    Year      Criteria
                  Male   1998 N        14507
                              Y         2308
                         1999 N        14119
                              Y         2331
                  

                  等等.我希望数字标准"列显示每个性别和年份的总数百分比 - 所以上面 1998 年的 N = 14507 和 Y = 2308 我将有 N = 86.27% 和 Y = 13.73%.

                  and so on. I would like the 'Number Criteria' column to show the % of the total for each gender and year - so instead of N = 14507 and Y = 2308 for 1998 above I'd have N = 86.27% and Y = 13.73%.

                  谁能建议如何做到这一点?

                  Can anyone advise how to do this?

                  推荐答案

                  这个问题是建议重复.借用接受的答案,这将起作用:

                  This question is a direct extension of the suggested duplicate. Borrowing from the accepted answer, this will work:

                  In [46]: namesgrouped.groupby(level=[0, 1]).apply(lambda g: g / g.sum())
                  Out[46]: 
                                        Number
                  Sex  Year Criteria          
                  Male 1998 N         0.588806
                            Y         0.411194
                       1999 N         0.579612
                            Y         0.420388
                       2000 N         1.000000
                  

                  <小时>

                  编辑:转换操作可能比应用更快:


                  Edit: a transform operation might be faster than apply:

                  namesgrouped / namesgrouped.groupby(level=[0, 1]).transform('sum')
                  

                  这篇关于如何在 pandas 中使用groupby根据另一列中的条件计算百分比/比例总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                    <tfoot id='eyJrU'></tfoot>

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

                      <legend id='eyJrU'><style id='eyJrU'><dir id='eyJrU'><q id='eyJrU'></q></dir></style></legend>
                        <tbody id='eyJrU'></tbody>

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

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