0
0
Pandasdata~30 mins

shift() for lagging data in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using shift() for Lagging Data in pandas
📖 Scenario: Imagine you are analyzing daily sales data for a small store. You want to compare each day's sales with the previous day's sales to see how sales change day by day.
🎯 Goal: You will create a pandas DataFrame with sales data, then use the shift() function to add a new column showing the previous day's sales. Finally, you will print the updated DataFrame.
📋 What You'll Learn
Create a pandas DataFrame with exact sales data for 5 days
Create a variable to hold the number of days to lag
Use the shift() function with the lag variable to create a new column
Print the final DataFrame showing original and lagged sales
💡 Why This Matters
🌍 Real World
Lagging data is useful in time series analysis to compare current values with past values, such as sales, stock prices, or weather data.
💼 Career
Data analysts and data scientists often use lagging data to find trends, calculate changes, and build predictive models.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with two columns: 'Day' and 'Sales'. Use these exact values: Days are ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] and Sales are [200, 220, 210, 230, 240].
Pandas
Need a hint?

Use pd.DataFrame with a dictionary containing the two lists for 'Day' and 'Sales'.

2
Set the lag variable
Create a variable called lag_days and set it to 1. This will represent how many days to look back for lagging.
Pandas
Need a hint?

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

3
Create lagged sales column using shift()
Use the shift() function on the 'Sales' column of sales_data with the lag value lag_days. Store the result in a new column called 'Previous_Sales' in the sales_data DataFrame.
Pandas
Need a hint?

Use sales_data['Sales'].shift(lag_days) to create the lagged sales and assign it to sales_data['Previous_Sales'].

4
Print the final DataFrame
Print the sales_data DataFrame to show the original sales and the previous day's sales side by side.
Pandas
Need a hint?

Use print(sales_data) to display the DataFrame.