I'm trying to get the mean from a pandas dataframe and convert into a new dataframe with the mean of a column grouped by month + year. So I found this answer: pandas dataframe groupby datetime month, but it didn't quite do the trick.
This is what I have:
dt grade
2020-01-01 10
2020-01-02 20
2020-02-01 30
2020-02-01 40
2020-03-01 10
2020-03-04 20
This is what I want:
dt grade_mean
2020-01 15
2020-02 35
2020-03 15
I would like to also be able to group by day or week, and also get a dataframe as the output. So this is what I've done so far:
df = pd.to_datetime(df['dt'], format="%m/%d/%y %I:%M%p")
df_grouped = df.groupby(by=[df.index.month, df.index.year])
df_grouped.grade.mean()
It's almost done, but I still can't get the dataframe.