【发布时间】:2021-11-03 17:56:54
【问题描述】:
- 我有一个数据集,它的字段包含文本信息(有单词和数字数据)。正如您在屏幕截图中看到的那样,有十进制数字。它们用逗号分隔,我需要确保它们之间有句点。
我之前尝试过编写一个正则表达式,但它会将文本中的所有逗号替换为句点。
Data_preprocessing['tweet_without_stopwords'] = Data_preprocessing['tweet_without_stopwords'].apply(lambda x: re.sub(",",'.', str(x)))
如何编写正则表达式,使其仅适用于数字的十进制表示法?也就是说,我想要在表单的文本中表达:number,number 在文本中是这样的number.number。
- 示例破坏了数据
Data_preprocessing['tweet_without_stopwords'] = Data_preprocessing['tweet_without_stopwords'].apply(lambda x: re.sub("(\d*)\.(\d*)","\1,\2", str(x)))
方块出现了:D
3.
Data_preprocessing['tweet_without_stopwords'] = Data_preprocessing['tweet_without_stopwords'].apply(lambda x: re.sub("(\d+)\,(\d+)","\1.\2", str(x)))
【问题讨论】:
-
类似
(\d*)\.(\d*)到\1,\2? -
是的,但我认为恰恰相反——从“,”改为“。”
-
@kostya ivanov 你想从例如去吗?
128,01到128.01还是相反?