python - 代码在 spyder (python 3,7) 中有效,但在 Jupyter Notebook 中无效
问题描述
以下代码适用于 spyder
import re
price_num = []
for row in df['price']:
price_no_nonnum = re.sub('[^0-9]','', row) # this code line works in spyder
price_num.append(int(price_no_nonnum))
在 Jupyter 笔记本中,我收到一个错误
import re
price_num = []
for row in df['price']:
price_no_nonnum = re.sub('[^0-9]','', row) # this code line gives an error in jupyter
price_num.append(int(price_no_nonnum))
Jupyter 出现以下错误
TypeErrorTraceback (most recent call last)
<ipython-input-13-b3f4fcbe9d89> in <module>()
3 price_num = []
4 for row in autos['price']:
----> 5 price_no_nonnum = re.sub("[^0-9]","", row)
6 price_num.append(int(price_no_nonnum))
7
/dataquest/system/env/python3/lib/python3.4/re.py in sub(pattern, repl, string, count, flags)
177 a callable, it's passed the match object and must return
178 a replacement string to be used."""
--> 179 return _compile(pattern, flags).sub(repl, string, count)
180
181 def subn(pattern, repl, string, count=0, flags=0):
TypeError: expected string or buffer
解决方案
我的猜测是这row
不是字符串,而是一些 Pandas 特定的数据类型。您可以尝试这样做并完全避免使用正则表达式:
price_num = []
for row in df['price']:
try:
price = int(row)
except ValueError:
pass
else:
price_no_nonnum = ''.join(c for c in str(row) if c.isdigit())
price = int(price_no_nonnum)
price_num.append(price)
推荐阅读
- excel - Excel VBA - 标题长度会导致运行时错误 1004?
- python - 允许对运行 node.js 脚本的 Flask 服务器的多个 GET 请求
- graphql - 在 Gatsby 中按不同值过滤
- c# - 在 C# 中每个循环多次将相同参数传递给方法的更简洁方法?
- encryption - 缺少用于解密文件的加密密钥。向您的团队询问您的主人......它在 ENV ['RAILS_MASTER_KEY'] 中。Platform.sh 部署中止,
- spring - Spring Boot Application 显示没有错误,但是 tomcat 未初始化且 application.properties 未使用
- grpc - 是否可以从服务器(而不是通过移动设备)向 Google Home 设备发送消息?
- c# - 为什么在 Xamarin Forms 中调用 Navigation.InsertPageBefore() 后导航对我不起作用?
- syntax-error - Applescript 应为“”,但发现脚本结尾
- android - atal 异常:java.lang.IndexOutOfBoundsException setSpan (4 ... 4) 结束于长度 3 仅发生在 Nexus 5X 设备上