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 the 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 everytime before calling this method to differentiate it from the Python’s default function otherwise, it will throw an error.
Syntax: Series.str.split(pat=None, n=-1, expand=False)
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
To download the CSV used in 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.
Example #1: 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.
As shown in the output image, the Team column is now having a list. The string was separated at the first occurrence of “t” and not on the later occurrence since the n parameter was set to 1 (Max 1 separation in a string).
Example #2: 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.
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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Split a text column into two columns in Pandas DataFrame
- Split a String into columns using regex in pandas DataFrame
- Python | Split list of strings into sublists based on length
- Split large Pandas Dataframe into list of smaller Dataframes
- Python | Split the Even and Odd elements into two different lists
- Python | Split nested list into two lists
- Python | Ways to split strings using newline delimiter
- Python | Split dictionary keys and values into separate lists
- Python | Split given string into equal halves
- Python | Split string into list of characters
- Python | Split tuple into groups of n
- Python | Split a list into sublists of given lengths
- Python | Split list into lists by particular value
- Python - Split Numeric String into K digit integers
- Python | Split strings in list with same prefix in all elements
- Python | Split Sublist Strings
- Python | Reversed Split Strings
- Python | Split CamelCase string to individual strings
- Python | Ways to split strings on Uppercase characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai