Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a Python code to calculate percentage change between Id and Age columns of the top 2 and bottom 2 values
Sometimes we need to calculate percentage changes between consecutive rows in specific columns of a DataFrame. The pct_change() method calculates the percentage change from the previous row, which is useful for analyzing trends in data.
Understanding Percentage Change
The pct_change() method computes the percentage change between the current and previous element. The formula is: (current - previous) / previous.
Example Dataset
Let's start by creating a sample DataFrame with Id and Age columns ?
import pandas as pd
df = pd.DataFrame({
"Id": [1, 2, 3, None, 5],
"Age": [12, 12, 14, 13, None],
"Mark": [80, 90, None, 95, 85]
})
print("Original DataFrame:")
print(df)
Original DataFrame:
Id Age Mark
0 1.0 12.0 80.0
1 2.0 12.0 90.0
2 3.0 14.0 NaN
3 NaN 13.0 95.0
4 5.0 NaN 85.0
Calculating Percentage Change
Now let's calculate the percentage change for Id and Age columns ?
import pandas as pd
df = pd.DataFrame({
"Id": [1, 2, 3, None, 5],
"Age": [12, 12, 14, 13, None],
"Mark": [80, 90, None, 95, 85]
})
# Calculate percentage change for Id and Age columns
pct_change = df[['Id', 'Age']].pct_change()
print("Percentage change for Id and Age columns:")
print(pct_change)
Percentage change for Id and Age columns:
Id Age
0 NaN NaN
1 1.000000 0.000000
2 0.500000 0.166667
3 NaN -0.071429
4 NaN NaN
Getting Top 2 Values
To get the top 2 percentage changes (first 2 rows), use slicing with [0:2] ?
import pandas as pd
df = pd.DataFrame({
"Id": [1, 2, 3, None, 5],
"Age": [12, 12, 14, 13, None],
"Mark": [80, 90, None, 95, 85]
})
print("Id and Age - top 2 values:")
top_2 = df[['Id', 'Age']].pct_change()[0:2]
print(top_2)
Id and Age - top 2 values: Id Age 0 NaN NaN 1 1.0 0.0
Getting Bottom 2 Values
To get the bottom 2 percentage changes (last 2 rows), use slicing with [-2:] ?
import pandas as pd
df = pd.DataFrame({
"Id": [1, 2, 3, None, 5],
"Age": [12, 12, 14, 13, None],
"Mark": [80, 90, None, 95, 85]
})
print("Id and Age - bottom 2 values:")
bottom_2 = df[['Id', 'Age']].pct_change()[-2:]
print(bottom_2)
Id and Age - bottom 2 values:
Id Age
3 0.000000 -0.071429
4 0.666667 0.000000
Complete Solution
Here's the complete code that displays both top 2 and bottom 2 percentage changes ?
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
"Id": [1, 2, 3, None, 5],
"Age": [12, 12, 14, 13, None],
"Mark": [80, 90, None, 95, 85]
})
print("Original DataFrame:")
print(df)
print("\nId and Age - top 2 values:")
print(df[['Id', 'Age']].pct_change()[0:2])
print("\nId and Age - bottom 2 values:")
print(df[['Id', 'Age']].pct_change()[-2:])
Original DataFrame:
Id Age Mark
0 1.0 12.0 80.0
1 2.0 12.0 90.0
2 3.0 14.0 NaN
3 NaN 13.0 95.0
4 5.0 NaN 85.0
Id and Age - top 2 values:
Id Age
0 NaN NaN
1 1.0 0.0
Id and Age - bottom 2 values:
Id Age
3 0.000000 -0.071429
4 0.666667 0.000000
Key Points
The first row always shows
NaNbecause there's no previous value to compareMissing values (
NoneorNaN) in calculations result inNaNPositive values indicate an increase, negative values indicate a decrease
Use
[0:2]for top 2 rows and[-2:]for bottom 2 rows
Conclusion
Use pct_change() to calculate percentage changes between consecutive rows. Combine with slicing to extract specific ranges like top 2 or bottom 2 values for focused analysis.
