简体   繁体   中英

create new column of incremental number based on 2 categorical columns pandas dataframe

I have a pandas dataframe with the columns username and phase . I want to create a separate column called count with incremental values.

The count will be based on how many times a username has appeared in a specific phase . How can I accomplish this efficiently? Any suggestion is appreciated.


    username  phase      count
0    andrew    1          1
1    andrew    1          2
2    alex      1          1
3    alex      2          1
4    andrew    1          3
5    cindy     3          1
6    alex      2          2


You can use cumcount after groupby on username and phase .

df['count'] = df.groupby(['username', 'phase']).cumcount()+1
print(df)

  username  phase  count
0   andrew      1      1
1   andrew      1      2
2     alex      1      1
3     alex      2      1
4   andrew      1      3
5    cindy      3      1
6     alex      2      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