Python lesson
Sales by month
January 350, February 330, March 510. Group by df["Date"].dt.month, not "Date": every date is different.
Try this lesson free →Example
Group by the month of each date, then total each month:
df.groupby(df["Date"].dt.month)["Revenue"].sum()1 350
2 330
3 510Key points
df.groupby(df["Date"].dt.month)One group for each month.
["Revenue"].sum()Then total each month, as before.
.pct_change()Each month against the one before: 0.07 means 7% up.
Put together: df.groupby(df["Date"].dt.month)["Revenue"].sum(), then .pct_change() for growth.
Practice
1. Build a line that totals revenue for each month.
df.groupby(df["Date"].dt.month)["Revenue"].sum()2. Build a line that shows each month's change on the month before.
monthly.pct_change()3. Find the total revenue for each month.
df.groupby(df["Date"].dt.month)["Revenue"].sum()4. Count the sales in each month.
df.groupby(df["Date"].dt.month)["Revenue"].count()5. Your manager asks: is revenue growing? Show each month's change on the month before, as a decimal.
df.groupby(df["Date"].dt.month)["Revenue"].sum().pct_change()Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →