Explode

Python · Reshaping & Workflow

Theory

Sometimes one cell holds a list - several skills per employee, several tags per product, items in an order. df.explode('skills') gives each list element its own row and repeats the other columns, so Asha with ['SQL', 'Python'] becomes two rows. The original index label is repeated for each new row (use ignore_index=True to renumber); an empty list becomes one row with NaN, and a plain value stays as one row.

Data often arrives as a delimited string instead of a list, such as 'SQL,Python'. Split it first: df.assign(skills=df['skills'].str.split(',')).explode('skills'), then strip spaces. You can explode several list columns of matching lengths together with explode(['a', 'b']). Once exploded, ordinary tools work: value_counts() for the most common skill, groupby for counts per person, or merge to a lookup table. To go back, df.groupby('name')['skills'].agg(list) or agg(', '.join).

Why we use it

List-valued cells cannot be filtered, counted or joined directly. explode normalises them into one value per row, the shape every pandas operation and every database table expects.

When to use it

Use explode whenever a column holds lists or delimited strings you need to analyse per item. Watch the row count: one row with 50 tags becomes 50 rows. In SQL this is LATERAL FLATTEN (Snowflake), UNNEST or CROSS APPLY STRING_SPLIT.

How it works

  1. Column holding lists or 'a,b,c' strings
  2. Is it a delimited string? Yes: Split it: .str.split(','). No: Already a list.
  3. explode('col'): one row per list element
  4. Strip spaces and drop NaN from empty lists
  5. One item per row, ready for value_counts / groupby

Syntax

df.explode('col', ignore_index=False)
df.assign(col=df['col'].str.split(',')).explode('col')
df.groupby('key')['col'].agg(list)

Example

skills = df.assign(skill=df['skills'].str.split(',')).explode('skill')
print(skills['skill'].value_counts())

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic