Skip to content Skip to sidebar Skip to footer

Pandas Dataframe.groupby Including Index

I have a dataset taken from the Windows Eventlog. The TimeGenerated column is set as the index. I'd like to get an aggregated view showing me the number of events, by EventType (in

Solution 1:

What I was missing is that you can perform a groupby() on one or more levels of your index.

test = log.set_index('EventType', append=True)
test = test.groupby(level=[0,1])['EventID'].count('EventID')
test.unstack().fillna(0)

Alternatively, the suggestion by Brian Pendleton worked as well:

pd.get_dummies(log.EventType)

The difference with this last approach is that it doesn't work as well if you need to add another level in your column axis (e.g. by Hostname). But that wasn't part of the original question of course.

Post a Comment for "Pandas Dataframe.groupby Including Index"