Change language

Python | Pandas Series.str.rpartition ()

|

Pandas str.rpartition() works similarly to str.partition () and str.split () . Instead of .rpartition() a line in each occurrence from the left, .rpartition() splits the line just once, and it’s too reverse (on the right side). Unlike .split () , the rpartition () method stores the delimiter / separator.

.str must be prefixed each time before calling this method to distinguish it from the default function in Python, otherwise it will throw an error.

Note. This method is different from the str method .partition (). Instead of splitting on first occurrence, the string is split on last occurrence of a delimiter / separator.

Syntax: Series.str.rpartition (pat = ’’, expand = True)

Parameters:
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 load the CSV used in the code, click here.

In the following examples, the data frame used contains data about some NBA players ... An image of the data frame before any operations is attached below. 

Example # 1: Splitting a string into a list

In this example, the Team column is split into a list at the last occurrence of & # 39; o & # 39 ;. The .dropna () method removes null lines before performing any operations to avoid errors.

# pandas module import

import pandas as pd 

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 
# remove null values ​​to avoid errors

data.dropna (inplace = True

  
# splitting and rewriting a column

data [ "Team" ] = data [ "Team" ]. str .rpartition ( "o" , False )

 
# display
data

Output:
As shown in the output image, the line & # 39; Boston Celtics & # 39; was separated in the last appearance of & # 39; o & # 39 ;. Also the returned list has a delimiter. 

Example # 2: Splitting a line into dataframe

This example splits the Name column into a dataframe at the last occurrence (right first) in a, while retaining the Expand option. The .dropna () method removes null lines before performing any operations to avoid errors.

# pandas module import

import pandas as pd 

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 
# remove null values ​​to avoid errors

data.dropna (inplace = True

  
# splitting and rewriting a column

df = data [ " Name " ]. str . rpartition ( "a" , True )

  
# display
df

Output:
As shown in the output image, the row was split into a dataframe at the last occurrence & # 39; a & # 39; in line. 

Note. If there is no separator in the line, the entire line is preserved in the last column of the data frame / last index of the list.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method