0
0
Data Analysis Pythondata~30 mins

Window functions (expanding, ewm) in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Analyzing Daily Sales Trends with Window Functions
📖 Scenario: You work in a small store that tracks daily sales. You want to understand how sales change over time and spot trends. Using window functions like expanding and exponentially weighted moving averages (ewm) helps you see the bigger picture beyond daily ups and downs.
🎯 Goal: Build a simple Python program that uses expanding and ewm window functions on daily sales data to calculate cumulative and smoothed sales values.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data
Add a configuration variable for the smoothing factor
Calculate expanding sum of sales
Calculate exponentially weighted moving average (ewm) of sales
Print the DataFrame with original and calculated columns
💡 Why This Matters
🌍 Real World
Stores and businesses use expanding sums to track total sales over time and ewm to smooth out daily fluctuations for better trend analysis.
💼 Career
Data analysts and scientists use window functions like expanding and ewm to prepare and analyze time series data for reports and decision-making.
Progress0 / 4 steps
1
Create daily sales data
Create a pandas DataFrame called sales_data with two columns: 'day' and 'sales'. Use these exact values: days 1 to 7, and sales values 100, 120, 130, 90, 150, 170, 160.
Data Analysis Python
Hint

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

2
Set smoothing factor for ewm
Create a variable called alpha and set it to 0.3. This will be the smoothing factor for the exponentially weighted moving average.
Data Analysis Python
Hint

Just create a variable named alpha and assign it the value 0.3.

3
Calculate expanding sum and ewm average
Add two new columns to sales_data: 'expanding_sum' which is the expanding sum of sales, and 'ewm_avg' which is the exponentially weighted moving average of sales using the alpha variable.
Data Analysis Python
Hint

Use expanding().sum() on the sales column for cumulative sum.
Use ewm(alpha=alpha).mean() on the sales column for smoothed average.

4
Print the sales data with calculations
Print the sales_data DataFrame to display the original sales, expanding sum, and ewm average columns.
Data Analysis Python
Hint

Use print(sales_data) to show the DataFrame with all columns.