Search code examples
pythonpandasdataframecolumnsorting

Increment dataframe column from a certain value based on condition


I have a dataframe and I want to create a new column based on a condition on a different column. Create the new column "ans" that starts with 2348 and increment based on the column "ix". In the "ix" column if the value is the same as the next one keep the "ans" column the same and if its different increment "ans"

Thank you for your answer

  index   ix  
    1     pa 
    2     pa
    3     pa
    4     pe
    5     fc
    6     pb
    7     pb
    8     df

should result in:-

  index   ix  ans
    1     pa   2348
    2     pa   2348
    3     pa   2348
    4     pe   2349
    5     fc   2350
    6     pb   2351
    7     pb   2351
    8     df   2352

Solution

  • You can check equality of ix column with it's previous row using shift(), and use cumsum():

    df['ans'] = (
        df['ix'].ne(df['ix'].shift(1))
        ).cumsum().add(2348) - 1
    

    prints:

       index  ix   ans
    0      1  pa  2348
    1      2  pa  2348
    2      3  pa  2348
    3      4  pe  2349
    4      5  fc  2350
    5      6  pb  2351
    6      7  pb  2351
    7      8  df  2352