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



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 non adjusted ewm:       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("adjusted ewm:\n",df.ewm(com=0.5).mean()) 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 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 non adjusted ewm:       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
Updated on: 2021-02-25T06:03:40+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements