pandas 只替换正则表达式 [英] Pandas replace only working with regex
问题描述
我已经找到了解决方法,但是我仍然对正在发生的事情感到好奇:
I already found a workaround, but I'm still curious about what's going on:
当我尝试像这样在熊猫中进行替换时:
When I try to do replace in Pandas like so:
merged4[['column1', 'column2', 'column3']] = merged4[['column1', 'column2', 'column3']].replace(to_replace='.', value=',')
它不起作用.例如,我尝试了Inplace的所有不同变体.由于列为float64,因此我也做了astype(str).
It's not working. I tried all different variants with Inplace for example. I also did astype(str) since the columns were float64.
但是,当我这样做时:
merged4[['column1', 'column2', 'column3']] = merged4[['column1', 'column2', 'column3']].replace(to_replace='\.', value=',', regex=True)
它就像魅力一样运作.
It's working like charm.
有什么问题吗?
推荐答案
当您不使用regex=True
时,该方法将查找replace_to值的完全匹配.当您使用regex=True
时,它也会寻找子字符串.因此,当您使用该参数时,您的代码即可工作.
例子
When you don't use regex=True
the method will look for the exact match of the replace_to value. When you use regex=True
it will look for the sub strings too. So your code works when you use that parameter.
Example
当您不使用regex=True
参数时,replace方法将查找系列中replace_to值的精确匹配.
When you dont use the regex=True
parameter, the replace method will look for the exact match of the replace_to value in the series.
df = pd.DataFrame()
df['k'] = ['YoYo.','mins.s.s','sos.s.','.','mama.mia']
df['k'].replace('.',',')
0 YoYo.
1 mins.s.s
2 sos.s.
3 ,
4 mama.mia
使用regex = True
时,它甚至与系列的子字符串匹配并更改字符串
When you use regex = True
it even matches the substrings of the series and changes the strings
df = pd.DataFrame()
df['k'] = ['YoYo.','mins.s.s','sos.s.','.','mama.mia']
df['k'].replace('\.',',',regex=True)
0 YoYo,
1 mins,s,s
2 sos,s,
3 ,
4 mama,mia
这篇关于 pandas 只替换正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!