Reputation: 1082
I have a dataframe like the follow:
Col1
0 C
1 A
3 D
4 A
5 A
I would like to count the step/index that a certain value will re-occur so I would get the following:
Col1 Col2
0 C NaN
1 A 2
3 D NaN
4 A 1
5 A NaN
Any ideas on how to do it ? Thanks for help !
Upvotes: 1
Views: 43
Reputation: 862511
Use GroupBy.cumcount
and then replace 0
to NaN
s:
df['Col2'] = df.groupby('Col1').cumcount(ascending=False).replace(0,np.nan)
print (df)
Col1 Col2
0 C NaN
1 A 2.0
3 D NaN
4 A 1.0
5 A NaN
Alternative solution with mask
:
df['Col2'] = df.groupby('Col1').cumcount(ascending=False).mask(lambda x: x == 0)
Upvotes: 4