3

Data Wrangling Solutions — Rolling Calculations In Pandas

 9 months ago
source link: https://ujjwal-dalmia.medium.com/data-wrangling-solutions-rolling-calculations-in-pandas-de112a16f1c5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Data Wrangling Solutions — Rolling Calculations In Pandas

A simple solution to calculate rolling difference & percentage change.

0*r4rwAFwOVAq7E2DP

Photo by Nathan Dumlao on Unsplash

One of the frequently used data pre-processing actions is to create rolling windows calculations. These calculations specifically come-in handy when working with time-series data. In this tutorial, we will look at the Pandas functions to implement these calculations. The image below will give a visual detail on what we are trying to achieve.

1*lzgCNkfjt3BH_wVw4SShjQ.png

Sample Scenario (Image by Author)

Assumption and Recommendation

Being hands-on is the key to master programming. We recommend that you continue to implement the codes as you follow through with the tutorial. The sample data and the associated Jupiter notebook is available in the Scenario_11 folder of this GitHub link.

If you are new to GitHub and want to learn it, please go through this tutorial. To set up a new Python environment on your system, please go through this tutorial.

Following is the list of Python concepts and pandas functions/ methods used in the tutorial:

Pandas functions

  • read_csv
  • shift
  • pct_change

Let’s get started

Step 1 — Keeping the data ready

For this tutorial, we have created a dataset that contains the population of India recorded at an interval of 5 years. The dictionary for this data set and the sample data snapshot is as follows:

  • Year — The year in which we recorded the population.
  • Population— The actual population itself.

Step 2 — Importing pandas package and the data set in Python.

Once you have the data available, the next step is to import it to your Python environment.

#### Sample Code#### Importing Pandas
import pandas as pd

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK