Remove or Delete First word of the column in Pandas

In This Section we will be focusing on how to remove or delete First word of the character column in pandas in two ways.

  • Remove or delete First word from left of the column in pandas python using split() based on first whitespace.
  • Remove or delete first 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 First word of the column in pandas 1

 

 

Remove or delete First word of the column in pandas:

Method1:

We will be using split() method and will be splitting the first whitespace with n=1 and then select the second list by indexing  as shown below which will in turn removes the first word of the column


#Use split by first whitespace with n=1 and then select second lists by indexing:
## Method 1:

df['Desc'] = df['Description'].str.split(n=1).str[1]
df

Result:

Remove or delete First word of the column in pandas 2

 

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 first word and there by removing or deleting the  first word of the column


#### Method 2: using lambda function
df['Desc'] = df['Description'].apply(lambda x: ' '.join(x.split(' ')[1:]))
df

Result:

Remove or delete First word of the column in pandas 2

 

                                                                                                         

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts