Reputation: 79
In below dataframe, I need to add +1 for all values which have 0:
col_a
0
a
0
b
0
c
The end result should look something like below:
col_a
1
a
2
b
3
c
I have tried 'for loops' but does not seem to work. Any suggestions?
Upvotes: 1
Views: 56
Reputation: 71689
Let us try cumsum
to create a sequential counter then update values in col_a
using boolean indexing:
m = df['col_a'].eq('0')
df.loc[m, 'col_a'] = m.cumsum()
col_a
0 1
1 a
2 2
3 b
4 3
5 c
Upvotes: 3