7

Data Wrangling Solutions — Cumulative Calculations In Pandas

 9 months ago
source link: https://ujjwal-dalmia.medium.com/data-wrangling-solutions-cumulative-calculations-in-pandas-53eba930df19
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 — Cumulative Calculations In Pandas

A simple solution to calculate the cumulative sum & product.

0*rW7BU8D9HLAw2xXq

Photo by Martin Sanchez on Unsplash

Other than creating rolling windows calculations, another data-wrangling challenge faced by budding data scientists is to perform cumulative calculations. 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*bWzDQP8RGoXiM0fQXWwSqA.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_12 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
  • cumsum
  • cummin
  • cummax
  • cumprod

Let’s get started

Step 1 — Keeping the data ready

For this tutorial, we have created a dataset that contains the monthly sales achieved by a local grocery store. The dictionary for this data set and the sample data snapshot is as follows:

  • Year — The year of sales.
  • Month— The month of the year.
  • Sales — Sales achieved in the month.

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