简体   繁体   中英

Find Keyword in Excel Sheet and Remove Any Word that Comes After

I am trying to have a column of names that are only one word long but I want python to search the excel sheet for that name and to drop any other values in that cell. For example:

"Informed Consent" would be in column 1 of my excel sheet. I want the script to be able to search the excel sheet for "Consent" and "informed" would be dropped out. The output would look like "Consent". I have tried using:

print (df.val.str.replace('Informed Consent/Assent', 'Informed Consent'))

But that will only search for the whole string 'informed consent/assent' and not just a keyword.

IIUC you can use an np.where() to look if the field as Consent present and if it does change the text to equal only Consent, or if not present then simply display the column as is:

df['Column1'] = np.where(df['Column1'].str.contains('Consent'), 'Consent', df['Column1'])

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