Skip to content
Advertisement

What is best way to loop through Pandas dataframe employing a sequentially counted value in each row where condition is true?

Business Problem: For each row in a Pandas data frame where condition is true, set value in a column. When successive rows meet condition, then increase the value by one. The end goal is to create a column containing integers (e.g., 1, 2, 3, 4, … , n) upon which a pivot table can be made. As a side note, there will be a second index upon which the pivot will be made.

Below is my attempt, but I’m new to using Pandas.

JavaScript

Advertisement

Answer

You can try:

JavaScript

This gives:

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