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)
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)
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
We can use str.replace
here:
df["Name"] = df["Name"].str.replace(r's+d{4} w+$', '', regex=True)
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)