Search code examples
pythonpandascounter

Increment counter the first time a number is reached


This is probably a very silly question. But, I'll still go ahead and ask. How would you increment a counter only the first time a particular value is reached?

For example, if I have step below as a column of the df and would want to add a counter column called 'counter' which increments the first time the 'step' column has a value of 6

enter image description here


Solution

  • You can use .shift() in pandas -

    Notice how you only want to increment if value of df['step'] is 6 and value of df.shift(1)['step'] is not 6.

    df['counter'] = ((df['step']==6) & (df.shift(1)['step']!=6 )).cumsum()
    print(df)
    

    Output

          step  counter
    0      2        0
    1      2        0
    2      2        0
    3      3        0
    4      4        0
    5      4        0
    6      5        0
    7      6        1
    8      6        1
    9      6        1
    10     6        1
    11     7        1
    12     5        1
    13     6        2
    14     6        2
    15     6        2
    16     7        2
    17     5        2
    18     6        3
    19     7        3
    20     5        3
    

    Explanation

    a. df['step']==6 gives boolean values - True if the step is 6

    0     False
    1     False
    2     False
    3     False
    4     False
    5     False
    6     False
    7      True
    8      True
    9      True
    10     True
    11    False
    12    False
    13     True
    14     True
    15     True
    16    False
    17    False
    18     True
    19    False
    20    False
    Name: step, dtype: bool
    

    b. df.shift(1)['step']!=6 shifts the data by 1 row and then checks if value is equal to 6.

    When both these conditions satisfy, you want to increment - .cumsum() will take care of that. Hope that helps!

    P.S - Although it's a good question, going forward please avoid pasting images. You can directly paste data and format as code. Helps the people who are answering to copy-paste