GoBlue_MathMan GoBlue_MathMan - 2 months ago 11
Python Question

Python Pandas Group by date using datetime data

I have a column

Date_Time
that I wish to groupby date time without creating a new column. Is this possible the current code I have does not work.

df = pd.groupby(df,by=[df['Date_Time'].date()])

Answer

Credit to @jezrael for his setup dataframe


You can set the index to be 'Date_Time' and use pd.TimeGrouper

df.set_index('Date_Time').groupby(pd.TimeGrouper('D')).mean().dropna()

enter image description here