0
0
Data Analysis Pythondata~30 mins

Shift and lag operations in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Shift and lag operations
📖 Scenario: You work in a sales department. You have daily sales data for a small store. You want to compare each day's sales with the previous day's sales to see if sales went up or down.
🎯 Goal: Build a small program that uses shift and lag operations on sales data to compare each day's sales with the previous day.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data.
Add a new column that shows the previous day's sales using shift.
Calculate the difference between current day sales and previous day sales.
Print the final DataFrame showing the comparison.
💡 Why This Matters
🌍 Real World
Shift and lag operations are used in time series analysis to compare current values with past values, such as daily sales, stock prices, or sensor readings.
💼 Career
Data analysts and data scientists use shift and lag operations to detect trends, calculate changes, and prepare data for forecasting models.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with two columns: 'date' and 'sales'. Use these exact values for 'date': '2024-04-01', '2024-04-02', '2024-04-03', '2024-04-04', '2024-04-05'. Use these exact values for 'sales': 100, 150, 130, 160, 170. Import pandas as pd first.
Data Analysis Python
Hint

Use pd.DataFrame with a dictionary containing the 'date' and 'sales' lists.

2
Add a column for previous day's sales
Create a new column in sales_data called 'prev_day_sales' that contains the previous day's sales using the shift(1) method on the 'sales' column.
Data Analysis Python
Hint

Use sales_data['prev_day_sales'] = sales_data['sales'].shift(1) to get the previous day's sales.

3
Calculate sales difference from previous day
Create a new column in sales_data called 'sales_diff' that calculates the difference between 'sales' and 'prev_day_sales'.
Data Analysis Python
Hint

Subtract sales_data['prev_day_sales'] from sales_data['sales'] and assign to sales_diff.

4
Print the final DataFrame
Print the sales_data DataFrame to show the 'date', 'sales', 'prev_day_sales', and 'sales_diff' columns.
Data Analysis Python
Hint

Use print(sales_data) to display the DataFrame.