Pandas Tricks - Split One Row Of Data Into Multiple Rows
As a data scientist or analyst, you will need to spend a lot of time wrangling the data from various sources so that you can have a standard data structure for your further analysis. There are cases that you get the raw data in some sort of summary view and you would need to split one row of data into multiple rows based on certain conditions in order to do grouping and matching from different perspectives. In this article, we will be discussing a solution to solve this particular issue.
Prerequisites:
You will need to get pandas installed if you have not yet. Below is the pip command to install pandas:
pip install pandas
And let’s import the necessary modules and use this sample data for our demonstration, you can download it into your local folder, or just supply this URL link to pandas read_excel method:
import pandas as pd
import numpy as np
df = pd.read_excel("eShop-Delivery-Record.xlsx", sheet_name=0)
So if we do a quick view of the first 5 rows of the data with df.head(5), you would see the below output: