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

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

      <i id='DuFXK'><tr id='DuFXK'><dt id='DuFXK'><q id='DuFXK'><span id='DuFXK'><b id='DuFXK'><form id='DuFXK'><ins id='DuFXK'></ins><ul id='DuFXK'></ul><sub id='DuFXK'></sub></form><legend id='DuFXK'></legend><bdo id='DuFXK'><pre id='DuFXK'><center id='DuFXK'></center></pre></bdo></b><th id='DuFXK'></th></span></q></dt></tr></i><div id='DuFXK'><tfoot id='DuFXK'></tfoot><dl id='DuFXK'><fieldset id='DuFXK'></fieldset></dl></div>
      <legend id='DuFXK'><style id='DuFXK'><dir id='DuFXK'><q id='DuFXK'></q></dir></style></legend>
      <tfoot id='DuFXK'></tfoot>
      1. Pandas 按递增顺序编号组内的行数

        Pandas number rows within group in increasing order(Pandas 按递增顺序编号组内的行数)

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

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

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

                • 本文介绍了Pandas 按递增顺序编号组内的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Given the following data frame:

                  import pandas as pd
                  import numpy as np
                  df=pd.DataFrame({'A':['A','A','A','B','B','B'],
                                  'B':['a','a','b','a','a','a'],
                                  })
                  df
                  
                      A   B
                  0   A   a 
                  1   A   a 
                  2   A   b 
                  3   B   a 
                  4   B   a 
                  5   B   a
                  

                  I'd like to create column 'C', which numbers the rows within each group in columns A and B like this:

                      A   B   C
                  0   A   a   1
                  1   A   a   2
                  2   A   b   1
                  3   B   a   1
                  4   B   a   2
                  5   B   a   3
                  

                  I've tried this so far:

                  df['C']=df.groupby(['A','B'])['B'].transform('rank')
                  

                  ...but it doesn't work!

                  解决方案

                  Use groupby/cumcount:

                  In [25]: df['C'] = df.groupby(['A','B']).cumcount()+1; df
                  Out[25]: 
                     A  B  C
                  0  A  a  1
                  1  A  a  2
                  2  A  b  1
                  3  B  a  1
                  4  B  a  2
                  5  B  a  3
                  

                  这篇关于Pandas 按递增顺序编号组内的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How can I flush the output of the print function (unbuffer python output)?(如何刷新打印函数的输出(取消缓冲 python 输出)?)
                  Remap values in pandas column with a dict, preserve NaNs(使用 dict 重新映射 pandas 列中的值,保留 NaN)
                  What#39;s the difference between `raw_input()` and `input()` in Python 3?(Python 3 中的 `raw_input()` 和 `input()` 有什么区别?)
                  When should I (not) want to use pandas apply() in my code?(我什么时候应该(不)想在我的代码中使用 pandas apply()?)
                  Split (explode) pandas dataframe string entry to separate rows(拆分(分解) pandas 数据框字符串条目以分隔行)
                    <bdo id='XE83I'></bdo><ul id='XE83I'></ul>

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

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

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