0
0
Pandasdata~15 mins

pct_change() for percentage change in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Calculate Percentage Change Using pct_change()
📖 Scenario: You work in a small shop and track daily sales. You want to see how sales change day to day in percentage to understand growth or decline.
🎯 Goal: Build a small program that creates a sales data table, sets a configuration for percentage change periods, calculates the percentage change using pct_change(), and prints the result.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data
Set a variable for the number of periods to calculate percentage change
Use the pct_change() method with the periods variable
Print the resulting DataFrame with percentage changes
💡 Why This Matters
🌍 Real World
Tracking percentage change in sales helps businesses understand growth trends and make better decisions.
💼 Career
Data analysts and business intelligence professionals often calculate percentage changes to report performance metrics.
Progress0 / 4 steps
1
Create sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with a column 'Sales' containing these exact values: 100, 120, 90, 150, 130.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where the key is 'Sales' and the value is the list of sales numbers.

2
Set periods for percentage change
Create a variable called periods and set it to 1 to calculate the percentage change compared to the previous day.
Pandas
Need a hint?

Just create a variable named periods and assign it the value 1.

3
Calculate percentage change
Create a new column in sales_data called 'Pct_Change' that stores the percentage change of 'Sales' using the pct_change() method with the periods variable.
Pandas
Need a hint?

Use sales_data['Sales'].pct_change(periods=periods) and assign it to sales_data['Pct_Change'].

4
Print the sales data with percentage change
Print the sales_data DataFrame to display the sales and their percentage changes.
Pandas
Need a hint?

Use print(sales_data) to show the DataFrame with the new column.