Search code examples
pythonpandasincrementauto-increment

auto numbering on a date change in dataframe


I would like to get auto incremental number in a data-frame whenever there is a change in a date. Any recommendations please. I would like the output as attached in screenshot. enter image description here


Solution

  • try this:

    df['Date'] = pd.to_datetime(df['Date'])
    df.assign(SINo=df.groupby('Date').cumcount()+1)
    
    Date    SINo
    0   2020-10-08  1
    1   2020-10-24  1
    2   2020-10-24  2
    3   2020-10-24  3
    4   2020-10-24  4
    5   2020-10-31  1
    6   2020-11-08  1
    7   2020-11-19  1
    8   2020-11-24  1
    9   2020-11-24  2
    10  2020-12-01  1
    11  2020-12-05  1