0
0
Data Analysis Pythondata~10 mins

Extracting date components (year, month, day) in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the year from the 'date' column in the DataFrame.

Data Analysis Python
df['year'] = df['date'].dt.[1]
Drag options to blanks, or click blank then click option'
Aday
Bhour
Cyear
Dmonth
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'month' or 'day' instead of 'year'.
Forgetting to use the dt accessor.
2fill in blank
medium

Complete the code to extract the month from the 'date' column in the DataFrame.

Data Analysis Python
df['month'] = df['date'].dt.[1]
Drag options to blanks, or click blank then click option'
Amonth
Bday
Cyear
Dminute
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' or 'year' instead of 'month'.
Forgetting to use the dt accessor.
3fill in blank
hard

Fix the error in the code to extract the day from the 'date' column.

Data Analysis Python
df['day'] = df['date'].[1]
Drag options to blanks, or click blank then click option'
Adt.day()
Bday
Cdate.day
Ddt.day
Attempts:
3 left
💡 Hint
Common Mistakes
Using date.day which is invalid for pandas Series.
Calling dt.day() as a function.
4fill in blank
hard

Fill both blanks to create a dictionary with years as keys and months as values from the 'date' column.

Data Analysis Python
date_dict = {df['date'].dt.[1][i]: df['date'].dt.[2][i] for i in range(len(df))}
Drag options to blanks, or click blank then click option'
Ayear
Bday
Cmonth
Dhour
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping year and month in keys and values.
Using day or hour instead of month.
5fill in blank
hard

Fill all three blanks to create a list of tuples (year, month, day) from the 'date' column.

Data Analysis Python
date_tuples = [(df['date'].dt.[1][i], df['date'].dt.[2][i], df['date'].dt.[3][i]) for i in range(len(df))]
Drag options to blanks, or click blank then click option'
Ayear
Bmonth
Cday
Dminute
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of year, month, day.
Using minute instead of day.