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?

This recipe helps you calculate MOVING AVG in a Pandas DataFrame

## Recipe Objective

Have to tried to do any mathematical function on all the values of a feature. Doing it manually may be a hectic work.

So this is the recipe on how can calculate moving average in a Pandas DataFrame.

## Step 1 - Import the library

``` import pandas as pd ```

We have only imported pandas which is needed.

## Step 2 - Creating dataframe

We have created a dictionary and passed the dictionary form pd.DataFrame to make a dataframe with various features. ``` raw_data = {"first_name": ["Sheldon", "Raj", "Leonard", "Howard", "Amy"], "last_name": ["Copper", "Koothrappali", "Hofstadter", "Wolowitz", "Fowler"], "age": [42, 38, 36, 41, 35], "Comedy_Score": [9, 7, 8, 8, 5], "Rating_Score": [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ["first_name", "last_name", "age", "Comedy_Score", "Rating_Score"]) print(df) ```

## Step 3 - Calculating moving Average

So here we have used rolling function with parameter window which signifies the number of rows the function will select to compute the statical measure. We have created a function which will calculate the mean.
We have calculated mean for two features and finally we have replaced nul values with zero. ``` df1 = df[["Comedy_Score","Rating_Score"]].rolling(window=2).mean() print(df1) df2 = df1.fillna(0) print(df2) ``` So the output comes as

```  first_name     last_name  age  Comedy_Score  Rating_Score
0    Sheldon        Copper   42             9            25
1        Raj  Koothrappali   38             7            25
2    Leonard    Hofstadter   36             8            49
3     Howard      Wolowitz   41             8            62
4        Amy        Fowler   35             5            70
Comedy_Score  Rating_Score
0           NaN           NaN
1           8.0          25.0
2           7.5          37.0
3           8.0          55.5
4           6.5          66.0
Comedy_Score  Rating_Score
0           0.0           0.0
1           8.0          25.0
2           7.5          37.0
3           8.0          55.5
4           6.5          66.0
```

#### Relevant Projects

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

##### Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

##### RASA NLU chatbot creation
The project will use rasa NLU for the Intent classifier, spacy for entity tagging, and mongo dB as the DB. The project will incorporate slot filling and context management and will be supporting the following intent and entities. Intents : product_info | ask_price|cancel_order Entities : product_name|location|order id The project will demonstrate how to generate data on the fly, annotate using framework and how to process those for different pieces of training as discussed above .

##### Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

##### Creating your own embeddings using Glove and Word2vec
We all at some point in time wished to create our own language as a child! But what if certain words always cooccur with another in a corpus? Thus you can make your own model which will understand which word goes with which one, which words are often coming together etc. This all can be done by building a custom embeddings model which we create in this project

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

##### Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

##### Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

##### Multi-Class Text Classification with Deep Learning using BERT
In this project, we will cover in detail the architecture of a transformer used in natural language processing use cases. We will go through the key nlp areas in the pre-transformer stage like bow, word2vec...and then the origin and gradual refinement of transformers. Finally, we will study one of the most popular state of the art transformer models, called BERT and use it for text classification on a large dataset.

##### 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.