Your question is similar to this question: pandas groupby without turning grouped by column into index
When you group by a column, the column you group by ceases to be a column, and is instead the index of the resulting operation. The index is not a column, it is an index. If you set as_index=False
, pandas keeps the column over which you are grouping as a column, instead of moving it to the index.
The second problem is the .agg()
function is also aggregating occ
over trip_departure_date
, and moving trip_departure_date
to an index. You don't need this second function to get the mean of occ
grouped by trip_departure_date
.
import pandas as pd
df1 = pd.read_csv("trip_departures.txt")

df1_agg = df1.groupby(['trip_departure_date'],as_index=False).mean()
Or if you only want to aggregate the occ
column:
df1_agg = df1.groupby(['trip_departure_date'],as_index=False)['occ'].mean()

df1_agg.plot(x = 'trip_departure_date', y = 'occ', figsize = (8,5), color = 'purple')
