简体   繁体   English

在 pandas dataframe 中,如何对字符串列的值进行正则表达式替换

[英]In pandas dataframe, how to do regex replace for values of a string column

Using Jupyter, I have three gender categories in a column (m, male and female), and I want to replace "m" by "male", when I try this code, also "female" become "femaleale".使用 Jupyter,我在一列中有三个性别类别(m,男性和女性),我想用“male”替换“m”,当我尝试这个代码时,“female”也变成了“femaleale”。

df['Gender'].replace(regex=[r'\bm\b'],value='male')

output output

male
femaleale
male

... ...

df['Gender'].replace(['m'], ['male'], inplace=True)

You can try this!!你可以试试这个!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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