How to remove the last 2 words in a dataframe; Pandas

Question:

I am filtering my data and I want to remove the year and location (Islamabad, Lahore, etc) in the ‘Name’ column and I don’t know what to do

This is the Picture

Answers:

We can use str.replace here:

df["Name"] = df["Name"].str.replace(r's+d{4} w+$', '', regex=True)
Answered By: Tim Biegeleisen

I tried the below and it did the job. Try it and let me know if you faced any problem:

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
def clean_name(name):
    name = re.sub(r'd{4}', '', name)
    
    name = re.sub(r's+[A-Z][a-z]+', '', name)
    
    return name.strip()
df['Name'] = df['Name'].apply(clean_name)
print("nCleaned DataFrame:")
print(df)
Answered By: Abdulmajeed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.