相当于Pandas Series / DataFrame的gsub

时间:2014-02-17 16:28:00

标签: r python-3.x

对于Python sub'gsubpandas,R的Series / DataFrame函数的等价物是什么?

例如,在R中,我的代码是

schData<-gsub("/"," by ",schData,ignore.case=F)

当上面的schData如下时,Python中的等效操作如下:

>>> type(schData)
Out[N]: pandas.core.series.Series

对于常规字符串,有re.sub函数,但这似乎只适用于字符串

2 个答案:

答案 0 :(得分:2)

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html

你要找的那种东西?

Python pandas equivalent for replace 有一些小例子。

PS:请在下次包含可重复的示例=)

答案 1 :(得分:0)

使用pandas replace和参数regex=Truethis answer中所述。

import pandas
s = pandas.Series(["ape", "monkey", "seagull"]) 

将“ a”替换为“ i”

s.replace("a", "i", regex=True)                                                                                                                                             
Out[4]: 
0        ipe
1     monkey
2    seigull
dtype: object