Home > other >  How to group by month and year from a specific range?
How to group by month and year from a specific range?

Time:04-20

The data have reported values for January 2006 through January 2019. I need to compute the total number of passengers Passenger_Count per month. The dataframe should have 121 entries (10 years * 12 months, plus 1 for january 2019). The range should go from 2009 to 2019.

The data

I have been doing:

df.groupby(['ReportPeriod'])['Passenger_Count'].sum()

But it doesn't give me the right result, it gives

Wrong result

CodePudding user response:

You can do

df['ReportPeriod'] = pd.to_datetime(df['ReportPeriod'])
out = df.groupby(df['ReportPeriod'].dt.strftime('%Y-%m-%d'))['Passenger_Count'].sum()

CodePudding user response:

Try this:

df.index = pd.to_datetime(df["ReportPeriod"], format="%m/%d/%Y")
df = df.groupby(pd.Grouper(freq="M")).sum()

  • Related