Home > database >  How to plot months of multiple years of a variable
How to plot months of multiple years of a variable

Time:03-14

hey i have the following dataset

Columns of the dataset

i already converted the orderdate column into a datetime column

now i want to plot the sales per month of each year showing month on x axis and sales on Y

df_grouped = df_clean.groupby(by = "ORDERDATE").sum()

how can i achive to just pull out data from each month in a specific year ?

thanks for helping out!

CodePudding user response:

You can create two additional columns from ORDERDATE, which are year and month by:

df_clean['year'] = df_clear['ORDERDATE'].dt.year
df_clean['month'] = df_clear['ORDERDATE'].dt.month

And then you can filter and group by these columns.

For example:

df_2022 = df_clean.loc[df_clean['year'] == '2022', :]
df_2022.groupby('month').sum()
  • Related