In This Section we will be focusing on how to remove or delete last word of the character column in pandas in two ways.
- Remove or delete last word from right of the column in pandas python using rsplit() based on last whitespace.
- Remove or delete last word of the column in pandas python in a roundabout way.
Let’s Look at these cases with Example,
Create Dataframe:
## create dataframe import pandas as pd d = {'Day' : ['day1','day2','day3','day4'], 'Description' : ['First day of the year', 'Second day of the year', 'Third day of the year', 'FOURTH day of the YEAR']} df=pd.DataFrame(d) df
Result dataframe is
Remove or delete last word of the column in pandas:
Method1:
We will be using rsplit() method and will be splitting the last whitespace with n=1 and then select the first list by indexing as shown below which will in turn removes the last word of the column
#Use rsplit() to split by last whitespace with n=1 and then select first lists by indexing: ## Method 1: df['Desc'] = df['Description'].str.rsplit(n=1).str[0] df
Result:
Method2:
We will be using split() method along with lambda which will split based on the space and join by indexing and leaving out the last word and there by removing or deleting the last word of the column
### Method 2: using lambda function df['Desc'] = df['Description'].apply(lambda x: ' '.join(x.split(' ')[:-1])) df
Result: