Skip to content
Advertisement

For Loop to populate Pandas dataframe

In below dataframe, I need to add +1 for all values which have 0:

JavaScript

The end result should look something like below:

JavaScript

I have tried ‘for loops’ but does not seem to work. Any suggestions?

Advertisement

Answer

Let us try cumsum to create a sequential counter then update values in col_a using boolean indexing:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement