0

I have a dataframe with the birthdays of the users of specific clients in a dataframe, and i want to
count occurences per month, but also per client, so in the end id have all the occurences of birthdates per month for client #1, all the occurences of birthdates per month for client #2 etc..., similarly to what is described in this question in which they suggest using this:

df.groupby([df['birthdate'].dt.year.rename('year'), df['birthdate'].dt.month.rename('month')]).agg({'count'})

for a scenario where one wants to count the birthdays per month (not also per client).

How could i go about doing this?

  • Hi and welcome on SO. It will be great if you can have a look at [ask] and then try to produce a [mcve]. – rpanai Jan 07 '22 at 18:30

1 Answers1

0

If you want to group by month, year and client, you'll need to include the client column within the groupby() and reset_index():

groupby([df['birthdate'].dt.year.rename('year'), df['birthdate'].dt.month.rename('month'), df['client']]).agg({'count'}).reset_index()

From there you can use sort_values() to sort your dataframe as you wish.

Adam DS
  • 468
  • 3
  • 10
  • this gives me the error "AttributeError: Can only use .dt accessor with datetimelike values" even though birthday is a daytime value – joao pereira Jan 07 '22 at 19:43
  • If you get that error your birthdate column is most likely not in datetime format. Try to assign that column that format for to_datetime(). df['birthdate'] = pd.to_datetime(df['birthdate']) – Adam DS Jan 07 '22 at 21:02