How To Split Rows In Excel Using Python

Working with Excel spreadsheets can be a time-consuming manual process, but Python’s vast library of packages makes it easier and more efficient to manipulate, analyze, and modify Excel files. In this tutorial, we’ll learn how to split rows in an Excel sheet using Python. To accomplish this, we will be using the popular Python library pandas and openpyxl.

Step 1: Install the required packages

In order to work with Excel files using Python, you’ll need to install two packages – pandas and openpyxl. You can install these packages using pip by running the following commands in your terminal:

These packages will give us the tools we need to work with Excel files in Python.

Step 2: Read the Excel file

After installing the necessary packages, we can start reading data from an Excel file using pandas.

Assume that we have a sample Excel file called example.xlsx with the following content:

Name    Age    City
John     25    New York
Alice    30    Los Angeles
Bob      22    Chicago
Eve      28    Boston

First, let’s import the required libraries and read the Excel file:

Replace the file_path variable with the path of your Excel file and sheet_name with the specific sheet that contains the data you want to split.

Step 3: Split rows based on a condition or a value

Now let’s split the rows into two separate DataFrames based on age. In this case, we want to separate people who are 25 or younger and those who are older than 25:

Here, we’re using the pandas DataFrame syntax to create two new DataFrames based on the condition of the Age column.

Step 4: Write the separated DataFrames to the Excel file

After splitting the rows, we can now write the separated DataFrames to our destination Excel file. We will use the openpyxl package to load the workbook and the ExcelWriter class from pandas to write the DataFrames to separate sheets:

This code will create two new sheets in the Excel file: ‘Younger than 26’ and ‘Older than 25’, with the respective DataFrames written to those sheets.

Full Code

Here’s the complete code to split rows in an Excel file using Python:



In this tutorial, we learned how to split rows in an Excel sheet using Python. This technique can be useful when organizing data into separate sheets based on certain conditions, making it easier to analyze and manipulate data.

By utilizing the power of Python packages such as pandas and openpyxl, we can automate splitting rows and save time when working with Excel files.