
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Write a program in Python to transpose the index and columns in a given DataFrame
- Given an example for adjusted trial balance
- How to align a column right-adjusted in MySQL?
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a program in Python to count the records based on the designation in a given DataFrame
- Write a Python code to rename the given axis in a dataframe
- Write a program in Python to perform flatten the records in a given dataframe by C and F order
- Write a program in Python to count the total number of leap years in a given DataFrame
- What is adjusted trial balance?
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a program in Python to select any random odd index rows in a given DataFrame

Advertisements