Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to plot a time series line chart using matplotlib.
Matplotlib
import matplotlib.pyplot as plt import pandas as pd dates = pd.date_range('20230101', periods=5) values = [10, 12, 15, 14, 13] plt.plot(dates, values) plt.xlabel('Date') plt.ylabel('Value') plt.title('Time Series Plot') plt.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.savefig() instead of plt.show() when you want to display the plot.
Forgetting to call plt.show() so the plot does not appear.
✗ Incorrect
The plt.show() function displays the plot window with the time series chart.
2fill in blank
mediumComplete the code to convert a string column to datetime format in pandas.
Matplotlib
import pandas as pd data = {'date': ['2023-01-01', '2023-01-02', '2023-01-03'], 'value': [5, 6, 7]} df = pd.DataFrame(data) df['date'] = pd.to_datetime(df['[1]'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to convert a non-date column like 'value'.
Not converting the date column before plotting or analysis.
✗ Incorrect
The 'date' column contains the date strings that need to be converted to datetime objects.
3fill in blank
hardFix the error in the code that resamples time series data to monthly frequency.
Matplotlib
import pandas as pd dates = pd.date_range('2023-01-01', periods=90) values = range(90) df = pd.DataFrame({'value': values}, index=dates) monthly = df.resample('[1]').mean()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'D' which means daily, not monthly.
Using 'Y' which means yearly, not monthly.
✗ Incorrect
The 'M' frequency string resamples data by calendar month, which is correct for monthly aggregation.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps dates to values only if the value is greater than 10.
Matplotlib
data = {'2023-01-01': 8, '2023-01-02': 12, '2023-01-03': 15}
filtered = {date: value for date, value in data.items() if value [1] 10} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which filters values less than 10.
Using '==' which filters values equal to 10.
✗ Incorrect
We want values greater than 10, so the condition is value > 10.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps dates to values only if the date is after '2023-01-01' and the value is less than 20.
Matplotlib
data = {'2023-01-01': 8, '2023-01-02': 12, '2023-01-03': 25}
filtered = { [1]: [2] for [3] in data.items() if [1] > '2023-01-01' and [2] < 20 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single variable instead of unpacking the tuple.
Mixing up key and value in the comprehension.
✗ Incorrect
We unpack items into date and value, then map date to value with conditions on both.