0
0
Pandasdata~30 mins

dt accessor for datetime properties in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Exploring Dates with dt Accessor in pandas
📖 Scenario: You work at a small event company. You have a list of event dates and want to learn more about them, like which month and day of the week each event falls on.
🎯 Goal: Build a small pandas program that creates a DataFrame with event dates, extracts the month and day of the week using the dt accessor, and prints the results.
📋 What You'll Learn
Create a pandas DataFrame with a column named event_date containing specific dates.
Create a variable called date_column that holds the event_date column from the DataFrame.
Use the dt accessor on date_column to create two new columns: month and day_of_week.
Print the final DataFrame showing the original dates and the extracted month and day of the week.
💡 Why This Matters
🌍 Real World
Event planners often need to analyze dates to schedule activities, send reminders, or find patterns in attendance.
💼 Career
Data analysts and scientists use datetime properties to prepare and explore time-based data for reports and decision-making.
Progress0 / 4 steps
1
Create the DataFrame with event dates
Create a pandas DataFrame called df with a column event_date containing these exact dates as strings: '2024-01-15', '2024-02-20', '2024-03-10', '2024-04-25', '2024-05-30'. Then convert the event_date column to datetime type using pd.to_datetime().
Pandas
Need a hint?

Use pd.DataFrame to create the table and pd.to_datetime() to convert the date strings.

2
Create a variable for the date column
Create a variable called date_column and assign it the event_date column from the DataFrame df.
Pandas
Need a hint?

Assign df['event_date'] to a new variable called date_column.

3
Extract month and day of week using dt accessor
Use the dt accessor on date_column to create two new columns in df: month with the month number, and day_of_week with the day name.
Pandas
Need a hint?

Use date_column.dt.month for month numbers and date_column.dt.day_name() for day names.

4
Print the final DataFrame
Write a print statement to display the DataFrame df showing the event_date, month, and day_of_week columns.
Pandas
Need a hint?

Use print(df) to show the full table with new columns.