0
0
Pandasdata~30 mins

ewm() for exponential moving average in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Calculate Exponential Moving Average with pandas ewm()
📖 Scenario: You work as a data analyst for a small retail company. You have daily sales data for a product over 10 days. Your manager wants to see the trend of sales but wants recent days to have more importance than older days. This is called an exponential moving average (EMA).EMA helps smooth out the sales data and highlights recent changes more clearly.
🎯 Goal: Build a small program that calculates the exponential moving average of daily sales using pandas ewm() method.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data for 10 days.
Set a span value to control the smoothing factor for EMA.
Use pandas ewm() method with the span to calculate the EMA.
Print the original sales and the EMA values.
💡 Why This Matters
🌍 Real World
EMA is widely used in sales analysis, stock market trends, and any time series data to smooth out noise and highlight recent changes.
💼 Career
Data analysts and data scientists use EMA to help businesses make decisions based on recent trends rather than raw noisy data.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with two columns: 'Day' and 'Sales'. Use these exact values for 'Day': 1 to 10, and for 'Sales': 200, 220, 215, 230, 240, 235, 250, 245, 260, 270.
Pandas
Need a hint?

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

2
Set the span for EMA calculation
Create a variable called span and set it to 3. This controls how much weight recent sales get in the exponential moving average.
Pandas
Need a hint?

Just create a variable named span and assign it the value 3.

3
Calculate the exponential moving average
Use the ewm() method on the 'Sales' column of sales_data with the span variable, then call mean() to calculate the EMA. Store the result in a new column called 'EMA' in sales_data.
Pandas
Need a hint?

Use sales_data['Sales'].ewm(span=span, adjust=False).mean() to calculate EMA and assign it to sales_data['EMA'].

4
Print the sales and EMA columns
Print the sales_data DataFrame showing only the 'Day', 'Sales', and 'EMA' columns.
Pandas
Need a hint?

Use print(sales_data[['Day', 'Sales', 'EMA']]) to show the data.