Search code examples
pythonpandasdataframecomparerows

pandas dataframe: compare value in one column with previous value


I have a pandas dataframe in which I want to add a column (col_new), which values depend on a comparison of values in a existing column (col_exist).

Existing column (type=objects) contains As and Bs. New column should count, starting with 1. If an A follows an A, the count should rise by one. If an A follows a B, the count should rise by one. If a B follows an A, the count should not rise. If a B follows a B, the count should not rise.

col_exist  col_new
    A       1
    A       2
    A       3
    B       3
    A       4
    B       4
    B       4
    A       5
    B       5

I am completely new to programming, so thank you in advance for your adequade answer.


Solution

  • Use eq and cumsum:

    df['col_new'] = df['col_exist'].eq('A').cumsum()
    

    output:

      col_exist  col_new
    0         A        1
    1         A        2
    2         A        3
    3         B        3
    4         A        4
    5         B        4
    6         B        4
    7         A        5
    8         B        5