Fundamentals 4 min read

How to Split Excel Cells into Multiple Rows with pandas explode()

This article demonstrates how to transform a single Excel column containing semicolon‑separated values into separate rows using pandas' explode() function, offering both an openpyxl‑based method and a concise pandas solution with code examples and step‑by‑step screenshots.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Split Excel Cells into Multiple Rows with pandas explode()

Introduction

Hello, I am a Python enthusiast. While working through a case study in a Python automation book, I needed to split a column of data into multiple rows, as shown in the original screenshot.

The goal is to achieve the layout shown in the second image.

Idea

Initially, some suggested using Excel's Text‑to‑Columns feature, but that only splits the column without propagating other row data. A quicker workaround would be copy‑paste, but a more programmatic solution is preferable.

Solution

Two approaches are presented.

1. Using the openpyxl library as described in the referenced book.

2. Using pandas' explode() function, which expands list‑like entries into separate rows.

import pandas as pd
df = pd.read_excel('keywords.xlsx')
# ['序号', '年份', '来源出版物名称', '索引关键字']
df.columns
# Split the '索引关键字' column by ';'
df.loc[:, ['索引关键字']] = df['索引关键字'].str.split(';')
# Expand the list into separate rows
df_exploded = df.explode(column='索引关键字')
print(df_exploded.head())

The resulting output matches the desired layout.

Testing with multiple rows also produces the correct transformation.

Both approaches satisfy the requirement.

Conclusion

This short tutorial shows how to handle Excel data that needs to be split into multiple rows by leveraging pandas' explode() function, providing a quick and reliable solution for similar data‑processing tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonpandasexplodedata-manipulation
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.