【发布时间】:2018-11-15 04:44:37
【问题描述】:
我正在尝试从使用以下代码提取的数字中删除逗号:
with requests.Session() as s:
url = 'https://www.zoopla.co.uk/for-sale/property/london/paddington/?q=Paddington%2C%20London&results_sort=newest_listings&search_source=home'
r = s.get(url, headers=req_headers)
soup = BeautifulSoup(r.content, 'lxml')
prices = []
for price in soup.find_all('a', {"class":"listing-results-price text-price"}):
prices.append(price.text)
if price is None:
print('none')
df['price'] = prices
df['price'] = df['price'].str.extract('(\d+([\d,]?\d)*(\.\d+)?)', expand=True) #remove extract numbers with commas
df['price'] = df['price'].replace(',','', inplace = True)
这将返回一列,其中所有值均为无。有没有办法消除这个 NoneType 错误?
在我运行最后一行之前,数据帧如下:
price
0 NaN
1 1,875,000
2 4,950,000
3 500,000
4 675,000
5 980,000
6 475,000
7 849,950
8 1,050,000
9 1,050,000
10 650,000
11 1,100,000
12 1,300,000
13 895,000
14 1,000,000
15 26,800,000
16 1,600,000
17 695,000
18 2,100,000
19 510,000
20 1,200,000
21 3,000,000
22 599,000
23 26,800,000
24 1,550,000
25 750,000
26 1,600,000
27 1,025,000
【问题讨论】:
-
你能告诉我们输入数据吗?
-
我建议您将 if 条件提前一行。 :-)
-
@HarvIpan 在尝试替换最后一行的逗号之前,我已经编辑了帖子以显示数据。
-
@cwallenpoole 不幸的是,值仍被返回为 None
-
使用
df['price'].replace(',','', inplace = True),您将替换inplace,它不会返回任何内容。
标签: python pandas beautifulsoup nonetype