简体   繁体   中英

Group by Consecutive Dates and Rank

I have a dataframe like this, and I want to create a new column called 'Rank' group by barcode and date with date condition must be either same days or have consecutive dates and it must go to Step C for each barcode. For example, Barcode B have the same date, but because it goes to step C again so its rank should be 2. Rank is not limited to (1,2), it should be 1,2,3,4, etc base on date

Date           Barcode         Step       Value
--------------------------------------------------
2014-03-04      A               C             2
2014-03-04      A               D             4
2014-03-05      A               E             3
2014-03-06      A               F             4
2014-03-08      A               C             3
2014-03-08      A               D             2 
2014-03-04      B               C             5
2014-03-04      B               D             6
2014-03-05      B               E             7  
2014-03-06      B               F             8
2014-03-06      B               C             9
Date           Barcode         Step       Value          Rank
---------------------------------------------------------------
2014-03-04      A               C             2          1
2014-03-04      A               D             4          1
2014-03-05      A               E             3          1
2014-03-06      A               F             4          1
2014-03-08      A               C             3          2
2014-03-08      A               D             2          2
2014-03-04      B               C             5          1
2014-03-04      B               D             6          1
2014-03-05      B               E             7          1
2014-03-06      B               F             8          1
2014-03-06      B               C             9          2

You can do this by grouping and using cumcount()

df = df.sort_values(['Barcode', 'Date'])
df['Rank'] = df.groupby(['Barcode', 'Step']).cumcount() + 1
df

Gives you:

Date     Barcode    Step    Value   Rank
2014-03-04  A   C   2   1
2014-03-04  A   D   4   1
2014-03-05  A   E   3   1
2014-03-06  A   F   4   1
2014-03-08  A   C   3   2
2014-03-08  A   D   2   2
2014-03-04  B   C   5   1
2014-03-04  B   D   6   1
2014-03-05  B   E   7   1
2014-03-06  B   F   8   1
2014-03-06  B   C   9   2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM