简体   繁体   中英

How to replace whole string which matches regex in a pandas series

When I try to replace a group of strings according to my regex in pandas series the replacement happens only for the part of string. I want the whole string to be replaced with my replacement text. I've attached the images for reference. If someone could help, it will be really helpful

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Try something like this if you not that familiar with regex:

df.loc[~df['Gender'].isin(['Man', 'Woman']), 'Gender'] = 'Other'

Try this:

df1['Gender'].str.replace(r'(Man)|(Woman)', 'Others')

If something is inside squre brackets '[ ]', then each of the characters are mapped separately. Go to python regex documentation for more details.

[ ] Used to indicate a set of characters. In a set:

Characters can be listed individually, eg [amk] will match 'a', 'm', or 'k'.

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