0
0
Pandasdata~5 mins

pct_change() for percentage change in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A0
B1
C2
D-1
If a DataFrame column has values [100, 110, 121], what is the percentage change from the first to the second value?
A10%
B11%
C21%
D1%
What will pct_change() return if the previous value is zero?
ANaN or infinity
BNegative value
CZero
DPrevious value
Which pandas object can use pct_change()?
ASeries only
BDataFrame only
CBoth Series and DataFrame
DNeither
How do you calculate percentage change comparing to two rows before in pandas?
Adf.pct_change(periods=1)
Bdf.pct_change(periods=-2)
Cdf.pct_change(periods=0)
Ddf.pct_change(periods=2)
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.