How to calculate MOVING AVG in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to calculate MOVING AVG in a Pandas DataFrame?

How to calculate MOVING AVG in a Pandas DataFrame?

This recipe helps you calculate MOVING AVG in a Pandas DataFrame

0
In [3]:
## How to calculate MOVING AVG in a Pandas DataFrame
def Kickstarter_Example_95():
    print()
    print(format('How to calculate MOVING AVG in a Pandas DataFrame','*^82'))
    import warnings
    warnings.filterwarnings("ignore")
    # load libraries
    import pandas as pd
    raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks',
                             'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts',
                             'Scouts', 'Scouts', 'Scouts'],
                'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd',
                            '2nd','1st', '1st', '2nd', '2nd'],
                'name': ['Miller', 'Jacobson', 'Bali', 'Milner', 'Cooze', 'Jacon',
                         'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'],
                'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
                'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
    df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name',
                                           'preTestScore', 'postTestScore'])
    print(); print(df)

    # Calculate Rolling Moving Average with Window of 2
    df1 = df[['preTestScore','postTestScore']].rolling(window=2).mean()
    print(); print(df1)

    df2 = df1.fillna(0)
    print(); print(df2)

Kickstarter_Example_95()
****************How to calculate MOVING AVG in a Pandas DataFrame*****************

      regiment company      name  preTestScore  postTestScore
0   Nighthawks     1st    Miller             4             25
1   Nighthawks     1st  Jacobson            24             94
2   Nighthawks     2nd      Bali            31             57
3   Nighthawks     2nd    Milner             2             62
4     Dragoons     1st     Cooze             3             70
5     Dragoons     1st     Jacon             4             25
6     Dragoons     2nd    Ryaner            24             94
7     Dragoons     2nd      Sone            31             57
8       Scouts     1st     Sloan             2             62
9       Scouts     1st     Piger             3             70
10      Scouts     2nd     Riani             2             62
11      Scouts     2nd       Ali             3             70

    preTestScore  postTestScore
0            NaN            NaN
1           14.0           59.5
2           27.5           75.5
3           16.5           59.5
4            2.5           66.0
5            3.5           47.5
6           14.0           59.5
7           27.5           75.5
8           16.5           59.5
9            2.5           66.0
10           2.5           66.0
11           2.5           66.0

    preTestScore  postTestScore
0            0.0            0.0
1           14.0           59.5
2           27.5           75.5
3           16.5           59.5
4            2.5           66.0
5            3.5           47.5
6           14.0           59.5
7           27.5           75.5
8           16.5           59.5
9            2.5           66.0
10           2.5           66.0
11           2.5           66.0

Relevant Projects

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.