...

/

Splitting a Column

Splitting a Column

Learn how to split columns using Python.

Introduction

The process of splitting a column involves breaking up a single column in a DataFrame into multiple columns. For example, we can split a Full Name column into the First Name and Last Name columns based on the space delimiter. We perform this operation using the str.split() method, which takes a delimiter as an input and returns a DataFrame with each element in the split separated into an individual column, i.e., df[['Last Name', 'First Name']] = df['Full Name'].str.split(' '). The goal of splitting a column is to make it easier to visualize and analyze data.

Split a column into two columns
Split a column into two columns

Splitting a single column

To split a single column into multiple columns, we use the split() method. In addition, by using the expand parameter, we can create new columns for each element in the split.

Press + to interact
main.py
employees-eu.csv
Name,Department,Location,Salary,Years of Experience,Country
Jane Anna,HR,Paris,50000,5,France
Luke Wena,IT,Stockholm,60000,10,Sweden
Clara Lyinn,Finance,Paris,75000,5,France
Dmitry Zablov,Marketing,Stockholm,80000,8,Sweden
Ella Dechamps,HR,Paris,55000,3,France
Franklin Weur,IT,Stockholm,65000,6,Sweden
Greta Sarah,Finance,Paris,60000,10,France
Hanna Him,Marketing,Berlin,75000,5,Germany
Tabarasov Igor,HR,Paris,80000,8,France
Cate Jasmine,IT,Stockholm,55000,3,Sweden
Caroline Katarina,Finance,Paris,65000,6,France
Mary Lars,Marketing,Stockholm,60000,10,Sweden
Pauline Mia,HR,Berlin,50000,5,Germany
Nina,IT,Stockholm,60000,10,Sweden
Oskar Yuri,Finance,Paris,75000,5,France
Petr Zakov,Marketing,Berlin,80000,8,Germany
Quinn Jane,HR,Paris,55000,3,France

Let’s review the code line by line:

    ...