<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 按递增顺序编号组内的行数

        时间:2024-04-20

              <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 按递增顺序编号组内的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:'else if' 的正确语法是什么? 下一篇:交错两个字符串的最pythonic方法

                  相关文章

                    <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>
                      <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>