w3resource

Pandas Series: str.partition() function

Series-str.partition() function

The str.partition() function is used to split the string at the first occurrence of sep.

This method splits the string at the first occurrence of sep, and returns 3 elements containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return 3 elements containing the string itself, followed by two empty strings.

Syntax:

Series.str.partition(self, sep=' ', expand=True)
Pandas Series: str.partition() function

Parameters:

Name Description Type/Default Value Required / Optional
sep   String to split on. str
Default Value: whitespace
Required
expand   If True, return DataFrame/MultiIndex expanding dimensionality. If False, return Series/Index. bool
Default Value: True
Required

Returns: DataFrame/MultiIndex or Series/Index of objects

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s

Output:

0    Albert Junior Labuschagne
1            Jason Pitt-Rivers
dtype: object
Pandas Series: str.partition() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s.str.partition()

Output:

        0	1	                 2
0	Albert		Junior Labuschagne
1	Jason		Pitt-Rivers

Example - To partition by the last space instead of the first one:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s.str.rpartition()

Output:

              0	1	         2
0	Albert Junior		Labuschagne
1	Jason		        Pitt-Rivers

Example - To partition by something different than a space:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s.str.partition('-')

Output:

                         0	   1	         2
0	Albert Junior Labuschagne		
1	              Jason Pitt	   -	        Rivers

Example - To return a Series containing tuples instead of a DataFrame:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s.str.partition('-', expand=False)

Output:

0    (Albert Junior Labuschagne, , )
1            (Jason Pitt, -, Rivers)
dtype: object

Example - Also available on indices:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.Index(['X 234', 'Y 888'])
idx

Output:

Index(['X 234', 'Y 888'], dtype='object')

Example - Which will create a MultiIndex:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.Index(['X 234', 'Y 888'])
idx.str.partition()

Output:

MultiIndex(levels=[['X', 'Y'], [' '], ['234', '888']],
           codes=[[0, 1], [0, 0], [0, 1]])

Example - Or an index with tuples with expand=False:

Python-Pandas Code:

import numpy as np
import pandas as pd
idx = pd.Index(['X 234', 'Y 888'])
idx.str.partition(expand=False)

Output:

Index([('X', ' ', '234'), ('Y', ' ', '888')], dtype='object')

Previous: Series-str.pad() function
Next: Series-str.repeat() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-str-partition.php