Open In App

Python | Pandas Split strings into two List/Columns using str.split()

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string. It works similarly to Python’s default split() method but it can only be applied to an individual string. Pandas <code>str.split() method can be applied to a whole series. .str has to be prefixed every time before calling this method to differentiate it from Python’s default function otherwise, it will throw an error. To work in google colab import the files before using the dataset.

In this article, we will learn about how we can split strings into two columns using str.split()

Python Pandas str.split() Method Syntax

Syntax: Series.str.split(pat=None, n=-1, expand=False)

Parameters: 

  • pat: String value, separator or delimiter to separate string at. 
  • n: Numbers of max separations to make in a single string, default is -1 which means all. 
  • expand: Boolean value, returns a data frame with different value in different columns if True. Else it returns a series with list of strings. 

Return Type: Series of list or Data frame depending on expand Parameter

Sample DataFrame

To download the CSV used in the code, click here. In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below. 

Split Strings in Pandas

Splitting String into List

In this data, the split function is used to split the Team column at every “t”. The parameter is set to 1 and hence, the maximum number of separations in a single string will be 1. The expand parameter is False and that is why a series with List of strings is returned instead of a data frame. Here, the Team column is now having a list. The string was separated at the first occurrence of “t” and not at the later occurrence since the n parameter was set to 1 (Max 1 separation in a string).  

Python3




# importing pandas module
import pandas as pd
 
# reading csv file from url
data = pd.read_csv("nba.csv")
 
# dropping null value columns to avoid errors
data.dropna(inplace=True)
 
# new data frame with split value columns
data["Team"] = data["Team"].str.split("t", n=1, expand=False)
 
# df display
data


Output

Making Separate Columns from String

In this example, the Name column is separated at space (” “), and the expand parameter is set to True, which means it will return a data frame with all separated strings in different columns. The Data frame is then used to create new columns and the old Name column is dropped using .drop() method. 

Python3




# importing pandas module
import pandas as pd
 
# reading csv file from url
data = pd.read_csv("nba.csv")
 
# dropping null value columns to avoid errors
data.dropna(inplace=True)
 
# new data frame with split value columns
new = data["Name"].str.split(" ", n=1, expand=True)
 
# making separate first name column from new data frame
data["First Name"] = new[0]
 
# making separate last name column from new data frame
data["Last Name"] = new[1]
 
# Dropping old Name columns
data.drop(columns=["Name"], inplace=True)
 
# df display
data


Output: As shown in the output image, a new data frame was returned by the split() function and it was used to create two new columns ( First Name and Last Name) in the data frame.

New Data frame

Data frame with Added columns out3-15

Split Column into Two Columns In Pandas using apply() Function

In Pandas, the apply() function proves valuable for implementing operations that involve splitting a single column value into multiple columns. To achieve this, the process typically involves incorporating a lambda function along with Series.str.split() within the apply() function provided by pandas. This allows us to effectively apply the splitting logic to each element in a specified DataFrame column, resulting in the creation of new columns.

Python3




import pandas as pd
 
# reading csv file from url with ',' delimiter
data = pd.read_csv("nba.csv")
 
# dropping null value columns to avoid errors
data.dropna(inplace=True)
 
# define a function to split a name into first and last names
def split_name(name):
    return pd.Series(name.split(" ", 1))
 
# apply the split_name function to the "Name" column using apply()
data[['First Name', 'Last Name']] = data['Name'].apply(split_name)
 
# dropping old "Name" column
data.drop(columns=["Name"], inplace=True)
 
# df display
print(data)


Output

out3-15



Last Updated : 01 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads