Grouping and auto increment based on columns in pandas

前端 未结 1 1260
说谎
说谎 2021-01-13 06:47

i have a pandas frame that looks like this:

Is there a way to add the numbers in the last column without having to iterate through the data frame?

I

相关标签:
1条回答
  • 2021-01-13 07:34
    In [10]: df['counter'] = df.groupby(['Product','SubProd']).cumcount()+1
    
    In [11]: df
    Out[11]: 
               Product SubProd  counter
    4/20/2012        A      BL        1
    4/27/2012        A      BL        2
    5/4/2012         A      BL        3
    5/11/2012        A      BL        4
    5/18/2012        A      BL        5
    4/20/2012        A      lk        1
    4/27/2012        A      lk        2
    5/4/2012         A      lk        3
    5/11/2012        A      lk        4
    5/18/2012        A      lk        5
    5/25/2012        A      lk        6
    10/31/2014       B      po        1
    11/7/2014        B      po        2
    11/14/2014       B      po        3
    11/21/2014       B      po        4
    11/28/2014       B      po        5
    
    0 讨论(0)
提交回复
热议问题