Start learning free

LESSON 21 OF 165 · MACHINE LEARNING: PANDAS

Dates

Practice

Every line in orders.csv has a date like "2026-11-07". pd.read_csv reads it as text: to pandas it is just characters, so it can't tell you the month or the weekday.

pd.to_datetime(column) reads each text in a column and gives back a column of real dates. On a date column, .dt opens the parts of each date: .dt.month gives back the month as a number from 1 to 12, .dt.year the year, and .dt.day_name() the weekday's name as text. For example, the year each customer joined:

customers = pd.read_csv("customers.csv")
customers["joined"] = pd.to_datetime(customers["joined"])
customers["year"] = customers["joined"].dt.year

For the date 2026-03-14, .dt.month gives 3 and .dt.day_name() gives "Saturday". .tail(3) is like .head(3), but gives back the last 3 rows.

With a month column, money per month is a group-by you know: keep the "paid" lines, group them by month and add up total. The starting code already makes total, the quantity times the unit_price of each line. Your program must print:

           date  month       day
1494 2026-11-07     11  Saturday
1495 2026-08-27      8  Thursday
1496 2026-08-27      8  Thursday
month
1       552
2       919
3      1580
4      2203
5      3020
6      4470
7      3754
8      5562
9     10137
10    11890
11    12891

Your task: turn date into real dates, make a month column and a day column with the weekday's name, print those three columns for the last 3 rows, then print the money of the paid lines per month.


Assignment
Follow the Coding Tutorial to turn the dates into real dates, take their month and weekday, and add up the paid money per month.


Hint
Turn date into dates first: .dt only works on a date column. day_name is a method, so it needs (); month is not, so it doesn't.


STEP 1 OF 7
    Ctrl + Enter to check