pythonpandasdataframe

Counting number of separate events in dataframe


I am trying to count the number of separate events in a Dataframe (not number of occurences)

Let's say i have this dataframe :

df = pd.DataFrame([1, 2, 2, 2, 1, 1, 1, 1, 2, 1], columns=["events"])

And i want to count the number of separate events. If i use

df.value_counts()

It' going to give me the number of occurences of each event : events

1         6
2         4
Name: count, dtype: int64.

But i want to count the number of separate events regardless of their lenght. The result should be something like this :

     events
1         3
2         2

I wanted to know if there is a built in function for this. Any help or advice would be greatly appreciated. Thank you


Solution

  • Craft a mask (with shift and ne) to remove the successive duplicates and value_counts:

    df.loc[df['events'].ne(df['events'].shift()), 'events'].value_counts()
    

    Output:

    events
    1    3
    2    2
    Name: count, dtype: int64