Window functions help us look at data in small parts or over time. They let us calculate things like averages that change as we move through the data.
0
0
Window functions (expanding, ewm) in Data Analysis Python
Introduction
To see how the average of sales changes day by day in a store.
To smooth noisy data like stock prices to see trends better.
To calculate running totals or averages in a list of numbers.
To analyze time series data where recent points matter more.
To compare each data point with all previous points in order.
Syntax
Data Analysis Python
df['column'].expanding().mean() df['column'].ewm(span=3).mean()
expanding() looks at all data from the start up to the current point.
ewm() means Exponentially Weighted Moving, which gives more weight to recent data.
Examples
This calculates the average from the first value up to each point.
Data Analysis Python
import pandas as pd data = [1, 2, 3, 4, 5] df = pd.DataFrame({'values': data}) # Expanding mean exp_mean = df['values'].expanding().mean() print(exp_mean)
This calculates a weighted average giving more importance to recent values.
Data Analysis Python
import pandas as pd data = [1, 2, 3, 4, 5] df = pd.DataFrame({'values': data}) # Exponentially weighted mean with span=2 ewm_mean = df['values'].ewm(span=2).mean() print(ewm_mean)
Sample Program
This program shows how to calculate the expanding and exponentially weighted averages of sales over dates.
Data Analysis Python
import pandas as pd # Create sample data sales = [10, 20, 15, 30, 25] dates = pd.date_range('2024-01-01', periods=5) df = pd.DataFrame({'date': dates, 'sales': sales}) df.set_index('date', inplace=True) # Calculate expanding mean of sales expanding_avg = df['sales'].expanding().mean() # Calculate exponentially weighted mean with span=3 ewm_avg = df['sales'].ewm(span=3).mean() print('Expanding mean:') print(expanding_avg) print('\nExponentially weighted mean:') print(ewm_avg)
OutputSuccess
Important Notes
Expanding windows always start from the first data point and grow with each step.
Exponentially weighted means react faster to recent changes than simple averages.
You can adjust the span in ewm() to control how much recent data matters.
Summary
Window functions help analyze data step-by-step or over time.
expanding() looks at all data up to now.
ewm() gives more weight to recent data for smoother trends.