0
0
Pandasdata~10 mins

Extracting day of week and hour in Pandas - 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 day of the week from the 'date' column.

Pandas
df['day_of_week'] = df['date'].dt.[1]
Drag options to blanks, or click blank then click option'
Asecond
Bdayofweek
Cminute
Dhour
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hour' instead of 'dayofweek' will extract the hour, not the day.
Trying to call a method like 'dayofweek()' instead of using the attribute.
2fill in blank
medium

Complete the code to extract the hour from the 'timestamp' column.

Pandas
df['hour'] = df['timestamp'].dt.[1]
Drag options to blanks, or click blank then click option'
Aday
Bmonth
Chour
Dyear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'day' or 'month' will extract wrong parts of the datetime.
Trying to call hour() as a method instead of using the attribute.
3fill in blank
hard

Complete the code to correctly extract the day of week from 'date_time'.

Pandas
df['day_of_week'] = df['date_time'].dt.[1]
Drag options to blanks, or click blank then click option'
Aminute
Bhour
Csecond
Ddayofweek
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses after dayofweek causes an error.
Confusing attributes with methods.
4fill in blank
hard

Fill both blanks to create a dictionary with day_of_week (0-6) as keys and record counts as values if count is greater than 3. Assume 'day_of_week' column exists.

Pandas
day_counts = {day: [1] for day in range(7) if [2]
Drag options to blanks, or click blank then click option'
A(df['day_of_week'] == day).sum()
B(df['day_of_week'] == day).sum() > 3
Cday
Dday > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the day itself as the condition instead of its count.
Using parentheses incorrectly in the comprehension.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase day names as keys, their record counts as values, only if count is greater than 4. Assume 'day_name' column exists.

Pandas
result = { [1]: [2] for day_name in df['day_name'].unique() if [3] }
Drag options to blanks, or click blank then click option'
Aday_name.upper()
B(df['day_name'] == day_name).sum()
C(df['day_name'] == day_name).sum() > 4
Dday_name.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Not filtering by count correctly.