1. <tfoot id='lrIrF'></tfoot>

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

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

      <i id='lrIrF'><tr id='lrIrF'><dt id='lrIrF'><q id='lrIrF'><span id='lrIrF'><b id='lrIrF'><form id='lrIrF'><ins id='lrIrF'></ins><ul id='lrIrF'></ul><sub id='lrIrF'></sub></form><legend id='lrIrF'></legend><bdo id='lrIrF'><pre id='lrIrF'><center id='lrIrF'></center></pre></bdo></b><th id='lrIrF'></th></span></q></dt></tr></i><div id='lrIrF'><tfoot id='lrIrF'></tfoot><dl id='lrIrF'><fieldset id='lrIrF'></fieldset></dl></div>
      • <bdo id='lrIrF'></bdo><ul id='lrIrF'></ul>
    2. 使用Pandas GroupBy和VALUE_COUNTS查找最常用的值

      时间:2024-08-22
      <i id='MCvAx'><tr id='MCvAx'><dt id='MCvAx'><q id='MCvAx'><span id='MCvAx'><b id='MCvAx'><form id='MCvAx'><ins id='MCvAx'></ins><ul id='MCvAx'></ul><sub id='MCvAx'></sub></form><legend id='MCvAx'></legend><bdo id='MCvAx'><pre id='MCvAx'><center id='MCvAx'></center></pre></bdo></b><th id='MCvAx'></th></span></q></dt></tr></i><div id='MCvAx'><tfoot id='MCvAx'></tfoot><dl id='MCvAx'><fieldset id='MCvAx'></fieldset></dl></div>

    3. <small id='MCvAx'></small><noframes id='MCvAx'>

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

                  <tbody id='MCvAx'></tbody>
              1. <tfoot id='MCvAx'></tfoot>

                <legend id='MCvAx'><style id='MCvAx'><dir id='MCvAx'><q id='MCvAx'></q></dir></style></legend>
                本文介绍了使用Pandas GroupBy和VALUE_COUNTS查找最常用的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用表中的两列。

                +-------------+--------------------------------------------------------------+
                |  Area Name  |                       Code Description                       |
                +-------------+--------------------------------------------------------------+
                | N Hollywood | VIOLATION OF RESTRAINING ORDER                               |
                | N Hollywood | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | N Hollywood | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | N Hollywood | ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT               |
                | Southeast   | ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT               |
                | West Valley | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | West Valley | CRIMINAL THREATS - NO WEAPON DISPLAYED                       |
                | 77th Street | RAPE, FORCIBLE                                               |
                | Foothill    | CRM AGNST CHLD (13 OR UNDER) (14-15 & SUSP 10 YRS OLDER)0060 |
                | N Hollywood | VANDALISM - FELONY ($400 & OVER, ALL CHURCH VANDALISMS) 0114 |
                +-------------+--------------------------------------------------------------+
                

                我正在使用Groupby和Value_Counts按区域名称查找代码说明。

                df.groupby(['Area Name'])['Code Description'].value_counts()
                

                有没有办法只查看每个区域名称的前‘n’个值?如果我将.nlargest(3)追加到上面的代码,它只返回一个区域名称的结果。

                +---------------------------------------------------------------------------------+
                | Wilshire     SHOPLIFTING-GRAND THEFT ($950.01 & OVER)                         7 |
                +---------------------------------------------------------------------------------+
                

                推荐答案

                使用value_counts结果中的head每组:

                df.groupby('Area Name')['Code Description'].apply(lambda x: x.value_counts().head(3))
                

                输出:

                Area Name                                                                
                77th Street  RAPE, FORCIBLE                                                  1
                Foothill     CRM AGNST CHLD (13 OR UNDER) (14-15 & SUSP 10 YRS OLDER)0060    1
                N Hollywood  CRIMINAL THREATS - NO WEAPON DISPLAYED                          2
                             VIOLATION OF RESTRAINING ORDER                                  1
                             ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT                  1
                Southeast    ASSAULT WITH DEADLY WEAPON, AGGRAVATED ASSAULT                  1
                West Valley  CRIMINAL THREATS - NO WEAPON DISPLAYED                          2
                Name: Code Description, dtype: int64
                

                这篇关于使用Pandas GroupBy和VALUE_COUNTS查找最常用的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何将groupby()和value_count()转换为多个饼图/条形图 下一篇:pandas GROUP BY AND SUM,但在超过一定数量时创建新行

                相关文章

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

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

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

                  <tfoot id='ZgHSh'></tfoot>