Reputation: 123
I would like to groupby and get the another column containing category of groupby: This is my input
x=pd.DataFrame({'A': ['a', 'b', 'a', 'c'], 'B': ['w', 'x', 'w', 'y']})
I expect this to be the output:
y=pd.DataFrame({'A': ['a', 'b', 'a', 'c'], 'B': ['w', 'x', 'w', 'y'], 'C': ['C0', 'C1', 'C0', 'C2']})
I have tried this:
'C' + nlp.groupby(['HCCTrack (S)', 'AdditionalCodes (S)']).cumcount().add(1).astype(str)
Upvotes: 0
Views: 22
Reputation: 863196
Use GroupBy.ngroup
instead GroupBy.cumcount
:
x['C'] = 'C' + x.groupby(['A', 'B']).ngroup().astype(str)
print (x)
A B C
0 a w C0
1 b x C1
2 a w C0
3 c y C2
Upvotes: 1