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

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

    1. <legend id='yBxMB'><style id='yBxMB'><dir id='yBxMB'><q id='yBxMB'></q></dir></style></legend>

      • <bdo id='yBxMB'></bdo><ul id='yBxMB'></ul>
      <tfoot id='yBxMB'></tfoot>

      1. get_dummies 和一起计数

        get_dummies and count together(get_dummies 和一起计数)

          <bdo id='9fgdD'></bdo><ul id='9fgdD'></ul>
        • <tfoot id='9fgdD'></tfoot><legend id='9fgdD'><style id='9fgdD'><dir id='9fgdD'><q id='9fgdD'></q></dir></style></legend>

              <small id='9fgdD'></small><noframes id='9fgdD'>

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

                  <tbody id='9fgdD'></tbody>
                • 本文介绍了get_dummies 和一起计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数据框,其中包含不同的案例"作为行,其中有一个 id 和一个类别:

                  I have a dataframe with different "cases" as rows, in which there's an id and a category:

                  df = DataFrame({ 'id':[1122,3344,5566,5566,3344,5566,1122,3344], 
                              'category':['health','transport','energy','energy','transport','transport','transport','energy']})
                  
                      category    id
                  0   health      1122
                  1   transport   3344
                  2   energy      5566
                  3   energy      5566
                  4   transport   3344
                  5   transport   5566
                  6   transport   1122
                  7   energy      3344
                  

                  我正在尝试找到一种既可以获取类别的虚拟变量并对其进行计数的好方法,所以通过上面的示例我会得到:

                  I'm trying to find a good way to both get dummies of the categories and also count them, so with the above example I would get this:

                       health  transport  energy
                  1122    1        1          0
                  3344    0        2          1
                  5566    0        1          2
                  

                  有什么想法吗?

                  推荐答案

                  你可以使用pivot_table() 方法:

                  In [71]: df.pivot_table(index='id', columns='category', aggfunc='size', fill_value=0)
                  Out[71]:
                  category  energy  health  transport
                  id
                  1122           0       1          1
                  3344           1       0          2
                  5566           2       0          1
                  

                  或:

                  In [76]: df.pivot_table(index='id', columns='category', aggfunc='size', fill_value=0).rename_axis(None, 1)
                  Out[76]:
                        energy  health  transport
                  id
                  1122       0       1          1
                  3344       1       0          2
                  5566       2       0          1
                  

                  这篇关于get_dummies 和一起计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How do I write a Django query with a subquery as part of the WHERE clause?(如何编写带有子查询的 Django 查询作为 WHERE 子句的一部分?)
                  Unable to retrieve gmail messages from any folder other than inbox (Python3 issue)(无法从收件箱以外的任何文件夹中检索 gmail 邮件(Python3 问题))
                  How are you planning on handling the migration to Python 3?(您打算如何处理向 Python 3 的迁移?)
                  Pandas - group by column and transform the data to numpy array(Pandas - 按列分组并将数据转换为 numpy 数组)
                  In Pandas how could I pivot select date columns into rows(在 Pandas 中,我如何将选择日期列转换为行)
                  Pandas: subtract one column from another in a pivot table( pandas :从数据透视表中的另一列中减去一列)
                • <i id='e8Uzk'><tr id='e8Uzk'><dt id='e8Uzk'><q id='e8Uzk'><span id='e8Uzk'><b id='e8Uzk'><form id='e8Uzk'><ins id='e8Uzk'></ins><ul id='e8Uzk'></ul><sub id='e8Uzk'></sub></form><legend id='e8Uzk'></legend><bdo id='e8Uzk'><pre id='e8Uzk'><center id='e8Uzk'></center></pre></bdo></b><th id='e8Uzk'></th></span></q></dt></tr></i><div id='e8Uzk'><tfoot id='e8Uzk'></tfoot><dl id='e8Uzk'><fieldset id='e8Uzk'></fieldset></dl></div>
                    <tbody id='e8Uzk'></tbody>
                        <bdo id='e8Uzk'></bdo><ul id='e8Uzk'></ul>
                        <legend id='e8Uzk'><style id='e8Uzk'><dir id='e8Uzk'><q id='e8Uzk'></q></dir></style></legend>
                            <tfoot id='e8Uzk'></tfoot>

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