<legend id='IkGul'><style id='IkGul'><dir id='IkGul'><q id='IkGul'></q></dir></style></legend>
  • <small id='IkGul'></small><noframes id='IkGul'>

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

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

        PANDA VALUE_COUNTS包含GROUP BY之前的所有值

        pandas value_counts include all values before groupby(PANDA VALUE_COUNTS包含GROUP BY之前的所有值)
      1. <small id='LAtjP'></small><noframes id='LAtjP'>

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

                    <tbody id='LAtjP'></tbody>

                  本文介绍了PANDA VALUE_COUNTS包含GROUP BY之前的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有以下数据帧:

                  df = pd.DataFrame([['a',1, -1], ['a', 1, -1], ['b', 0, -1], ['c', -1, -1]] ,columns = ['col1', 'col2', 'col3'])
                  df
                      col1    col2    col3
                  0   a       1       -1
                  1   a       1       -1
                  2   b       0       -1
                  3   c       -1      -1
                  
                  现在我想按列对df进行分组,并分别计算col1列中的值出现的次数。

                  groupby_df = df.groupby('col1') 
                  for a,b in groupby_df:
                      print("{0} -> 
                  {1}".format(a, b['col1'].value_counts().sort_index()))
                  

                  我得到:

                  a -> 
                  a    2
                  Name: col1, dtype: int64
                  b -> 
                  b    1
                  Name: col1, dtype: int64
                  c -> 
                  c    1
                  Name: col1, dtype: int64
                  

                  但是我想单独统计出现的次数,并且仍然包括所有列值,如下所示:

                  a -> 
                  a    2
                  b    0
                  c    0
                  Name: col1, dtype: int64
                  b -> 
                  a    0
                  b    1
                  c    0
                  Name: col1, dtype: int64
                  c -> 
                  a    0
                  b    0
                  c    1
                  Name: col1, dtype: int64
                  

                  如有任何帮助,我们将不胜感激!

                  推荐答案

                  尝试使用.reindex():

                  import pandas as pd
                  
                  df = pd.DataFrame([['a',1, -1], ['a', 1, -1], ['b', 0, -1], ['c', -1, -1]] ,columns = ['col1', 'col2', 'col3'])
                  
                  # Create index using unique values of col1.
                  
                  uniques = pd.Index(df['col1'].unique())
                  
                  # Group.
                  
                  groupby_df = df.groupby('col1')
                  
                  # Use reindex to assign and autoamtically align the value counts with the index.
                  
                  for a, b in groupby_df:
                      print(b['col1'].value_counts().sort_index().reindex(uniques, fill_value = 0))
                  

                  给予:

                  a    2
                  b    0
                  c    0
                  Name: col1, dtype: int64
                  a    0
                  b    1
                  c    0
                  Name: col1, dtype: int64
                  a    0
                  b    0
                  c    1
                  Name: col1, dtype: int64
                  

                  这篇关于PANDA VALUE_COUNTS包含GROUP BY之前的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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替换为非空值)

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

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

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

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