Rahul Ramaswamy
Rahul Ramaswamy

Reputation: 53

How to group by month and find count using python pandas

I am trying to group the data by month (Created Date) and count the number of tuples for that month using python pandas. enter image description here

Upvotes: 1

Views: 824

Answers (2)

Corralien
Corralien

Reputation: 120399

Are you looking for:

# Convert to datetime64 if it's not already the case
df['Created Date'] = pd.to_datetime(df['Created Date'])

df.resample('MS', on='Created Date')['Created Date'].count()

Upvotes: 1

DeepKling
DeepKling

Reputation: 154

You could use

grouped = df.groupby(df["Created Date"].dt.strftime("%Y-%m")).size()

.dt.strftime allows for formatting the date as text, in this case year-month (%Y is the four digit year, %m the month)

Upvotes: 1

Related Questions