python - 正则表达式替换预期的字符串或类似字节的对象
问题描述
我有以下代码,我通过 Pandas 导入了一个数据集,并试图用逗号替换数字(例如,"12,000"
),但我似乎总是遇到错误"TypeError: expected string or bytes-like object"
df = pd.read_csv("C:/Users/Dell/Downloads/osc_samples_without.csv")
df2=df.loc[:,['Id','Description']]
df['Description'] = df['Description'].apply(lambda x:re.sub(r'(?<=\d)[,\.]','', df2))
我是 Python 和 Regex 的新手,因此将不胜感激。
解决方案
您可以replace
直接使用而无需re
显式使用:
df2['Description'] = df2['Description'].str.replace(r'(?<=\d)[.,]', '')
这里,
(?<=\d)
- 与紧接在数字前面的位置相匹配的正向后视[.,]
- 匹配一个.
或,
。
推荐阅读
- c++ - C++ 中的声明结构 C++ Prog。Lang 第 4 版 Bjarne 第 153/154 页
- c# - ASP.NET Core MVC ActionFilter 在 AuthorizationFilter 之前执行
- android - 如何为网站快捷方式(主屏幕)配置应用程序名称?
- javascript - 无法永远卸载nodejs
- sql - 如何在这种情况下检测重复以考虑内部差异?
- java - 在为对象创建参数时使用逗号分隔符 (Java)
- java - JSON 字符串到 CLOB 对象
- xml - 从备份数据重建 JPEG 图像
- linux - 如何从 linux 控制台创建 gcloud 数据流
- c# - ftp web 响应日期时间格式