Recall & Review
beginner
What does the
pct_change() function in pandas do?It calculates the percentage change between the current and a prior element in a pandas Series or DataFrame. This helps to see how much values have increased or decreased in percentage terms.
Click to reveal answer
beginner
How do you calculate the percentage change between rows in a DataFrame using pandas?
Use
df.pct_change(). By default, it calculates the percentage change between the current row and the previous row for each column.Click to reveal answer
intermediate
What does the
periods parameter in pct_change() control?It controls how many rows (or periods) back to compare the current value to. For example,
periods=2 compares the current row to the row two steps before.Click to reveal answer
intermediate
If a value in the previous row is zero, what will
pct_change() return for that row?It will return
inf (infinity) or NaN because division by zero is undefined in percentage change calculation.Click to reveal answer
intermediate
How can you use
pct_change() to calculate monthly percentage changes in a time series DataFrame?Set the DataFrame index to a datetime type and use
df.pct_change(periods=1) if data is monthly. For other frequencies, adjust periods accordingly.Click to reveal answer
What is the default number of periods
pct_change() compares when calculating percentage change?✗ Incorrect
By default,
pct_change() compares the current row with the previous row, which means periods=1.If a DataFrame column has values [100, 110, 121], what is the percentage change from the first to the second value?
✗ Incorrect
Percentage change = (110 - 100) / 100 = 0.10 or 10%.
What will
pct_change() return if the previous value is zero?✗ Incorrect
Division by zero is undefined, so pandas returns NaN or infinity.
Which pandas object can use
pct_change()?✗ Incorrect
pct_change() works on both Series and DataFrame objects.How do you calculate percentage change comparing to two rows before in pandas?
✗ Incorrect
Setting
periods=2 compares the current row to the row two steps before.Explain how the
pct_change() function works in pandas and give an example of when you might use it.Think about comparing current and previous values to see growth or decline.
You got /3 concepts.
Describe how you would handle a situation where the previous value is zero when using
pct_change().Consider what happens mathematically when dividing by zero.
You got /3 concepts.