# Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given dataframe

PythonPandasServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Assume, you have a dataframe and the result for adjusted and non-adjusted EWM are −

adjusted ewm:
Id       Age
0 1.000000 12.000000
1 1.750000 12.750000
2 2.615385 12.230769
3 2.615385 13.425000
4 4.670213 14.479339
Id       Age
0 1.000000 12.000000
1 1.666667 12.666667
2 2.555556 12.222222
3 2.555556 13.407407
4 4.650794 14.469136

## Solution

To solve this, we will follow the steps given below −

• Define a dataframe

• Calculate adjusted ewm with delay 0.5 using df.ewm(com=0.5).mean().

df.ewm(com=0.5).mean()
• Calculate non-adjusted ewm with delay 0.5 using df.ewm(com=0.5).mean().

df.ewm(com=0.5,adjust=False).mean()

### Example

import numpy as np
import pandas as pd
df = pd.DataFrame({'Id': [1, 2, 3, np.nan, 5],
'Age': [12,13,12,14,15]})
print(df)
print("non adjusted ewm:\n",df.ewm(com=0.5,adjust=False).mean())

### Output

Id Age
0 1.0 12
1 2.0 13
2 3.0 12
3 NaN 14
4 5.0 15
Id       Age
0 1.000000 12.000000
1 1.750000 12.750000
2 2.615385 12.230769
3 2.615385 13.425000
4 4.670213 14.479339
4 4.650794 14.469136