user11102368
user11102368

Reputation:

Pandas: How to group by time for each value in other column

I hava a dataframe like so:

column-one   column-two      column-3     column-4     column-5    date
 aaa           qqq             cat1         dsj           dak     2010-01-01 20:00:00
 ooo           www             cat2         fnk           qwe     2011-01-02 19:00:00
 oll           wee             cat2         fek           wqw     2011-03-02 22:00:00

Column-3 contains the categories in the dataframe. There are approximately 10-12 individual categories. For each category I am trying to count the number of times it occurs for each time(hour/date etc.) in the 'date' column. I ultimately want to be able to graph my results for each category individually. As well as being able to store my results in the dataframe.

This problem has stumped me for quite a while. If anyone has any suggestions please let me know. Or if you need anymore information. Thanks!

Upvotes: 0

Views: 48

Answers (2)

Polkaguy6000
Polkaguy6000

Reputation: 1208

It's a little difficult to understand your question. This answer is responding to your comment for @Sina Shabani. If you want to get this information for only one column at a time, you'd use:

 col_val_i_want = 'cat1' # Define what you want
 mask = df['column-3'].eq(col_val_i_want) # Create a filter
 df[mask].groupby('date').count() # Group by and get the count

Upvotes: 0

ilearn
ilearn

Reputation: 193

I think you might be looking for this?

 df.groupby(['date', 'column-3']).size()

Upvotes: 1

Related Questions