Recall & Review
beginner
What does the
rolling() function in pandas do?It creates a moving window over data, allowing calculations like sums or averages on subsets of data that slide over the dataset.
Click to reveal answer
beginner
How do you specify the size of the moving window in
rolling()?You set the window size by passing an integer to
rolling(window=), which defines how many data points are included in each window.Click to reveal answer
intermediate
What happens if the window size is larger than the available data points at the start?
By default, the result is
NaN for those positions because there isn't enough data to fill the window.Click to reveal answer
beginner
How can you calculate the moving average of a pandas Series using
rolling()?Use
series.rolling(window).mean() where window is the size of the moving window.Click to reveal answer
intermediate
What is the difference between
rolling() and expanding() in pandas?rolling() uses a fixed-size window that moves over data, while expanding() uses an increasing window starting from the first data point.Click to reveal answer
What does
df['col'].rolling(window=3).sum() compute?✗ Incorrect
The rolling sum with window=3 calculates the sum of each group of 3 consecutive values as the window moves.
If you want to avoid
NaN values at the start when using rolling(), what parameter can you use?✗ Incorrect
Setting
min_periods=1 allows calculations with fewer data points than the window size, reducing NaN at the start.What does the
center=True option do in rolling()?✗ Incorrect
With
center=True, the window is centered on the current row instead of being right-aligned.Which of these is NOT a valid aggregation function used with
rolling()?✗ Incorrect
plot() is not an aggregation function; rolling() expects functions like mean, sum, median.What type of data structure does
rolling() work on in pandas?✗ Incorrect
rolling() works on both pandas Series and DataFrame objects.Explain how the
rolling() function works in pandas and give an example of calculating a moving average.Think about how you slide a small window over your data to calculate averages.
You got /4 concepts.
Describe the difference between
rolling() and expanding() windows in pandas.One window size stays the same, the other grows as you move through data.
You got /3 concepts.