How to Split dataframe column by delimiter in Python : Methods

Split dataframe column by delimiter in Python

Pandas dataframe has many inbuilt functions that allow you to manipulate dataframe in an efficient way. If you want to split a dataframe column by delimiter then this post is for you. In this tutorial, you will know how to split pandas dataframe columns by delimiter in Python using various methods.

Create a Sample Dataframe

Before going to the methods section let’s first create a sample dataframe that will be used to implement these methods. You can create any data to dataframe using the pandas.DataFrame(). And if you have already datasets in a CSV file then you can read them using the read_csv() function.

Let’s create a dummy dataframe using the below lines of code.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA+01","UK+44","France+33","Germany+49"]}
df = pd.DataFrame(data)
df

Output

Sample dataframe for implementing splitting of dataframe column
Sample dataframe for implementing splitting of dataframe column

Method to split dataframe column by delimiter in Python

Let’s know all the methods to split dataframe columns by delimiter. The delimiter can be a comma, dash, or any special character. In the above dataframe country column contain a ‘+’ delimiter. I want to split it and make a column for dialing_code for it.  You can achieve that using the below methods.

Method 1: Using str.split() function

The first method is the use of str.split() function. It accepts the delimiter as an argument for splitting. To make new columns I will also pass the expand=True to expand the results.

Run the below lines of code to create a new column with the dialing_code.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA+01","UK+44","France+33","Germany+49"]}
df = pd.DataFrame(data)
df[['country', 'dialing_code']] = df['country'].str.split('+', expand=True)
df

Output

splitting dataframe column by delimiter in Python using split() function
splitting dataframe column by delimiter in Python using the split() function

Method 2: Split dataframe column by a delimiter in Python using apply() function

You can also use the apply() function with lambda to split the column. Here the apply() function will apply the lambda function on each row and split the string using the delimiter.

Use the below lines of code to split the dataframe column by a delimiter.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA+01","UK+44","France+33","Germany+49"]}
df = pd.DataFrame(data)
df[['dialing_code','country']] = df['country'].apply(lambda x: x.split("+"))
df

Output

splitting dataframe column by delimiter in Python using split() function
splitting dataframe column by delimiter in Python using apply() function

Conclusion

You can easily split the column of a dataframe using the delimiter. These are the methods to split the dataframe column. In this tutorial, you have learned the two methods split() and apply() function to split the column.

That’s all for now. I hope you have liked this tutorial. If you have any queries or doubts then you can contact us for more help,

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner