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.
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.
Name,Department,Location,Salary,Years of Experience,CountryJane Anna,HR,Paris,50000,5,FranceLuke Wena,IT,Stockholm,60000,10,SwedenClara Lyinn,Finance,Paris,75000,5,FranceDmitry Zablov,Marketing,Stockholm,80000,8,SwedenElla Dechamps,HR,Paris,55000,3,FranceFranklin Weur,IT,Stockholm,65000,6,SwedenGreta Sarah,Finance,Paris,60000,10,FranceHanna Him,Marketing,Berlin,75000,5,GermanyTabarasov Igor,HR,Paris,80000,8,FranceCate Jasmine,IT,Stockholm,55000,3,SwedenCaroline Katarina,Finance,Paris,65000,6,FranceMary Lars,Marketing,Stockholm,60000,10,SwedenPauline Mia,HR,Berlin,50000,5,GermanyNina,IT,Stockholm,60000,10,SwedenOskar Yuri,Finance,Paris,75000,5,FrancePetr Zakov,Marketing,Berlin,80000,8,GermanyQuinn Jane,HR,Paris,55000,3,France
Let’s review the code line by line: