We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Event Counting

The most fundamental question in event analytics: how many times did the thing happen?

Counting sounds trivial, but it often comes down to how you slice and group the data. Given our "events" DataFrame and event_type column, we can count in several ways. By event type:

print(events["event_type"].value_counts())

Which gives you:

page_view    4521
login        1893
purchase      312
signup        287
Name: event_type, dtype: int64

The value_counts() method gives you the overall distribution. Are most events page views? That's normal. Are most events purchases? Something is probably wrong with your data... there's no way your boss's product is that popular...

Count Per User

You can use the groupby() method to count the total events by user:

print(events.groupby("user_id")["event_type"].count())

Which prints:

user_id
101    73
202    23
532    45
...
Name: event_type, dtype: int64

You can also add a filter to count specific event types per user:

print(events[events["event_type"] == "purchase"].groupby("user_id").size())

Which prints:

user_id
101    23
202    5
...
dtype: int64

The difference between count() and size() trips people up. .count() excludes NaN values, while .size() counts everything. For event counting, .size() is usually what you want.

Assignment

SnackStack's product team wants a quick snapshot of event activity: how many of each event type, plus which one happens most.

Complete the get_event_summary function. It accepts an events DataFrame with an event_type column and returns a dictionary mapping each event type to its count, with an extra "most_common" key.