简体   繁体   中英

How to remove unwanted string from column?

One of my columns df["reviews"] has reviews in it but only some reviews end with the string "Was this information helpful?...."
So if my row contains this string, I want to get rid of the last 42 characters [:-42] which includes Was this information helpful?

how can I do this in Pandas
Tried this but it isn't working

def remove_unwanted(a):
    if "Was this information helpful" in a:
        print(a[:-42])   
    else:
        print("False")
        
# column without yes and no in complaint body
df['cleaned_reviews'] = df.apply(lambda row: remove_unwanted(row['reviews']), axis = 1)

试试这个,我想这就是你要找的

df['cleaned_reviews'] = df['reviews'].str.rstrip('Was this information helpful?')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM