Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
str.partition() works in a similar way like
str.split(). Instead of splitting the string at every occurrence of separator/delimiter, it splits the string only at the first occurrence. In the split function, the separator is not stored anywhere, only the text around it is stored in a new list/Dataframe. But in the
str.partition() method, the separator is also stored.
.str has to be prefixed every time before calling this method to differentiate it from the Python’s default function otherwise, it will throw an error.
Syntax: Series.str.partition(pat=’ ‘, expand=True)
pat: String value, separator or delimiter to separate string at. Default is ‘ ‘ (whitespace)
expand: Boolean value, returns a data frame with different value in different columns if True. Else it returns a series with list of strings. Default is True.
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 employees. The image of data frame before any operations is attached below.
Example #1: Splitting String into List
In this example, the Name column is splitted at the first occurrence of ‘, ‘. The expand parameter is kept False as to expand it into a list instead of Data Frame.
As shown in the output image, the Name column was splitted into list at first occurrence of ‘, ‘. As it can be seen, ‘, ‘ is also stored as an separate element of list.
Note: Do not get confused by two commas in the list, one is element and the other is element separator.
Example #2: Splitting String into Data frame
In this example, the First Name and Last name is separated from the Name column and stored into separate columns in the data frame.
As shown in the output image, the Name column was separated into a data frame with 3 columns(one of string before comma, and string after comma). After that data frame was used to create new columns in the same data frame. Old Name column was dropped using .drop() method.
New Data frame-
Data frame with Added columns-
- Python | pandas.map()
- Python | Pandas Series.sem()
- Python | Pandas dataframe.all()
- Python | Pandas Series.dt.day
- Python | Pandas dataframe.cov()
- Python | Pandas PeriodIndex.second
- Python | Pandas dataframe.max()
- Python | Pandas dataframe.mad()
- Python | Pandas Panel.sum()
- Python | Pandas dataframe.take()
- Python | Pandas dataframe.mean()
- Python | Pandas.apply()
- Python | Pandas dataframe.div()
- Python | Pandas Series.agg()
- Python | Pandas Panel.abs()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.