0
0
Pandasdata~15 mins

Extracting year, month, day in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Extracting year, month, day from dates using pandas
📖 Scenario: You work in a company that tracks employee joining dates. You have a list of joining dates and want to analyze the year, month, and day separately to find trends.
🎯 Goal: Build a pandas DataFrame with joining dates, then extract the year, month, and day into new columns.
📋 What You'll Learn
Create a pandas DataFrame with a column named joining_date containing specific dates.
Create a variable called date_column that holds the string 'joining_date'.
Use pandas datetime accessor to extract year, month, and day from the joining_date column into new columns named year, month, and day.
Print the final DataFrame to show the extracted columns.
💡 Why This Matters
🌍 Real World
Extracting year, month, and day from dates helps businesses analyze trends over time, like employee joining patterns or sales by month.
💼 Career
Data analysts and data scientists often need to break down dates to perform time-based analysis and reporting.
Progress0 / 4 steps
1
Create the DataFrame with joining dates
Import pandas as pd and create a DataFrame called df with a column joining_date containing these exact dates as strings: '2023-01-15', '2022-12-05', '2021-07-23', '2020-03-30'.
Pandas
Need a hint?

Use pd.DataFrame and pass a dictionary with key 'joining_date' and the list of date strings as value.

2
Create a variable for the date column name
Create a variable called date_column and set it to the string 'joining_date'.
Pandas
Need a hint?

Just assign the string 'joining_date' to the variable date_column.

3
Extract year, month, and day into new columns
Convert the joining_date column to datetime using pd.to_datetime. Then create three new columns in df: year, month, and day by extracting these parts from the datetime column using the pandas datetime accessor .dt and the variable date_column.
Pandas
Need a hint?

Use pd.to_datetime to convert the column, then use .dt.year, .dt.month, and .dt.day to get parts.

4
Print the DataFrame with extracted columns
Print the DataFrame df to display the joining_date, year, month, and day columns.
Pandas
Need a hint?

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