0
0
Data Analysis Pythondata~10 mins

Date feature extraction 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 convert the 'date' column to datetime format.

Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': ['2023-01-01', '2023-02-15', '2023-03-20']})
df['date'] = pd.to_datetime(df['date'][1])
Drag options to blanks, or click blank then click option'
A()
B[]
C{}
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Forgetting to call the function.
2fill in blank
medium

Complete the code to extract the year from the 'date' column into a new column 'year'.

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

Fix the error in extracting the weekday name from the 'date' column.

Data Analysis Python
df['weekday'] = df['date'].dt.[1]()
Drag options to blanks, or click blank then click option'
Aweekday
Bweekday_name
Cday_name
Dday
Attempts:
3 left
💡 Hint
Common Mistakes
Using weekday_name() which does not exist.
Using weekday which returns an integer.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 4 letters.

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
lengths = { [1] : [2] for word in words if len(word) > 4 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length as key instead of value.
Not filtering words longer than 4 letters.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, but only for words with length greater than 3.

Data Analysis Python
words = ['sun', 'moon', 'star', 'sky', 'planet']
result = { [1] : [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase words as keys.
Using wrong comparison operator.
Not using length correctly.