简体   繁体   English

基于2个分类列pandas数据框创建新的增量列

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

I have a pandas dataframe with the columns username and phase .我有一个熊猫数据框,其中包含usernamephase列。 I want to create a separate column called count with incremental values.我想用增量值创建一个名为count的单独列。

The count will be based on how many times a username has appeared in a specific phase . count将基于username在特定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 .您可以在usernamephase的 groupby 之后使用cumcount

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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