Home > Software engineering >  Convert to datetime to then groupby month
Convert to datetime to then groupby month

Time:12-05

I have data (labeled as df) that looks like this(below) where the 'DATE' column is formatted as such -> MM/DD/YYYY.

data table

I then need to group the available accident data by month.

Anyone know how to do this by first converting 'DATE' column to a datetime type, setting the index using set_index and then using groupby to group by month?

CodePudding user response:

Maybe this will help yo How do I convert a datetime to date? I looked at your table and it looks like you could just sor the table by date and you'd be fine

CodePudding user response:

If your column is not is not already in datetime:

df['Date'] = pd.to_datetime(df['Date'])

Then, do following to groupby month:

df.groupby(df.Date.dt.month)[columns_you_need_to_groupby_on].mean()
  • Related