如何检查字符串是否在$符号后包含数字字符(不包括熊猫中的空格)

时间:2019-05-11 14:31:16

标签: python pandas

我正在使用它来检查是否存在$符号,并将其替换。

string = '\$'
df= df[df['text'].str.contains(string,case=False)]    
df['text'] = df['text'].str.replace(string, " dollar ",regex=True)

但是我只想替换如果下一个字符是数字(空格除外)

示例df

id   text
1    buy for $1230
2    buy for $1,230
3    buy for $ 1230
4    buy for $ 1,230
5    buy for $abc

预期输出:

id   text
1    buy for dollar 1230
2    buy for dollar 1,230
3    buy for dollar 1230
4    buy for dollar 1,230
5    buy for $abc

编辑:如果我也知道检查If the next character is number (including space)

的解决方案,那将会很好

1 个答案:

答案 0 :(得分:3)

您可以使用positive lookaheads,以便$仅在后面跟有一个或多个空格的数字之前被替换:

df['text'] = (df['text'].str.replace(r'(\$)(?=\d|(?:\s+(?=\d)))', 
             " dollar ", regex=True))

   id   text
0   1   buy for dollar 1230
1   2   buy for dollar 1,230
2   3   buy for dollar  1230
3   4   buy for dollar  1,230
4   5   buy for $abc