【发布时间】:2018-05-28 12:40:13
【问题描述】:
使用 Pandas 删除字符串中除最后一个句点之外的所有句点,如下所示:
s = pd.Series(['1.234.5','123.5','2.345.6','678.9'])
counts = s.str.count('\.')
target = counts==2
target
0 True
1 False
2 True
3 False
dtype: bool
s = s[target].str.replace('\.','',1)
s
0 1234.5
2 2345.6
dtype: object
然而,我想要的输出是:
0 1234.5
1 123.5
2 2345.6
3 678.9
dtype: object
替换命令和掩码目标似乎正在删除未替换的值,我不知道如何解决这个问题。
【问题讨论】:
标签: python regex string pandas