小编law*_*son的帖子

将 Pandas 列与一长串元组进行比较

假设我们有一长串由坐标组成的元组:

coords = 
[(61.0, 73, 94.0, 110.0),
(61.0, 110.0, 94.0, 148.0), 
(61.0, 148.0, 94.0, 202.0), 
(61.0, 202.0, 94.0, 241.0).......]
Run Code Online (Sandbox Code Playgroud)

我们的数据框有多个列,包括对应于坐标的“左、上、左1、上1”。

    left    top     left1   top1
0   398     57.0    588     86
1   335     122.0   644     145
2   414     150.0   435     167
3   435     150.0   444     164
4   444     150.0   571     167
...     ...     ...     ...     ...
Run Code Online (Sandbox Code Playgroud)

我想检查哪些行落在这些坐标内。我目前正在一次执行一个元组,如下所示,但这非常慢。

for coord in coords:
    result = df.loc[(df['left']>(coord[0])) &
                    (df['left1']<=(coord[2])) & 
                    (df['top1']>(coord[1])) & 
                    (df['top']<(coord[3]))]
Run Code Online (Sandbox Code Playgroud)

有没有更快的方法来做到这一点?

非常感谢所有贡献者!

python pandas

5
推荐指数
1
解决办法
66
查看次数

将find_all美丽的汤标签组合成一个字符串

我正在使用beautifulsoup和html解析器进行抓取,并选择了要使用的html部分并将其保存为“容器”。

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import ssl

my_url = 'https://www._________.co.uk/'
context = ssl._create_unverified_context()
uClient = uReq(my_url, context=context)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div",{"class":"row"})
Run Code Online (Sandbox Code Playgroud)

当涉及到在跨度中彼此相邻的几个标签时,我面临挑战。

我可以通过使用

company_string = container.span.find_all("b")
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

[<b>Company</b>, <b>Name</b>, <b>Limited</b>]
Run Code Online (Sandbox Code Playgroud)

如何抛弃标签并将它们组合成字符串,以便输出为“ Company Name Limited”?

原始html在这里:

<span class="company">
<a href="/cmp/Company-Name-Limited" onmousedown="this.href = 
appendParamsOnce(this.href, 'xxxx')" rel="noopener" target="_blank">
<b>Company</b> <b>Name</b> <b>Limited</b>
</a>
</span>
Run Code Online (Sandbox Code Playgroud)

html python beautifulsoup web-scraping

1
推荐指数
1
解决办法
471
查看次数

标签 统计

python ×2

beautifulsoup ×1

html ×1

pandas ×1

web-scraping ×1