小编pol*_*ist的帖子

使用 pandas read_html 提取 href

作为我工作的一部分,我需要定期查看此页面以获取特定文件。我发现我可以使用 pandas 的方法read_html成功地将表读入数据框(这很方便,因为我可以通过关键字轻松查询特定文档)。我现在遇到的问题是这种方法无法解析我需要的链接,而是保存纯文本(特别是我指的是第二列,其中包含诸如“1682/0/15-19”之类的数字)。

我想出的代码非常简单:

import pandas as pd

df = pd.read_html('http://www.vru.gov.ua/act_list')[0]
Run Code Online (Sandbox Code Playgroud)

这给了我一个数据框,其中包含我需要的所有信息,除了链接。

是否有可能以某种方式获取链接而不是纯文本,如果是这样,我该怎么做?

我知道如果我使用了 Requests 和 BeautifulSoup 库,就有可能获得 href 链接,但我不知道 BeautifulSoup 库是否足以做到这一点。有什么提示还是我应该学习 BeautifulSoup?

html beautifulsoup web-scraping pandas python-requests

7
推荐指数
2
解决办法
4919
查看次数

如何保存异步请求的 JSON 响应?

我有一个关于异步请求的问题:

如何response.json()即时保存到文件?

我想发出请求并将响应保存到文件中.json,而不将其保留在内存中。


import asyncio
import aiohttp


async def fetch(sem, session, url):
    async with sem:
        async with session.get(url) as response:
            return await response.json() # here


async def fetch_all(urls, loop):
    sem = asyncio.Semaphore(4) 
    async with aiohttp.ClientSession(loop=loop) as session:
        results = await asyncio.gather(
            *[fetch(sem, session, url) for url in urls]
        )
        return results


if __name__ == '__main__':

    urls = (
        "https://public.api.openprocurement.org/api/2.5/tenders/6a0585fcfb05471796bb2b6a1d379f9b",
        "https://public.api.openprocurement.org/api/2.5/tenders/d1c74ec8bb9143d5b49e7ef32202f51c",
        "https://public.api.openprocurement.org/api/2.5/tenders/a3ec49c5b3e847fca2a1c215a2b69f8d",
        "https://public.api.openprocurement.org/api/2.5/tenders/52d8a15c55dd4f2ca9232f40c89bfa82",
        "https://public.api.openprocurement.org/api/2.5/tenders/b3af1cc6554440acbfe1d29103fe0c6a",
        "https://public.api.openprocurement.org/api/2.5/tenders/1d1c6560baac4a968f2c82c004a35c90",
    ) 

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete(fetch_all(urls, loop))
    print(data)
Run Code Online (Sandbox Code Playgroud)

目前,该脚本仅打印 JSON 文件,一旦它们全部被删除,我就可以保存它们: …

python python-asyncio aiohttp

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

不同长度的两个数据帧的列之间的余弦相似度?

我在 df1 中有文本列,在 df2 中有文本列。df2 的长度将与 df1 的长度不同。我想根据 df2[text] 中的每个条目计算 df1[text] 中每个条目的余弦相似度,并为每个匹配项打分。

样本输入

df1                           
mahesh                 
suresh


df2                                                                                  
surendra    
mahesh    
shrivatsa    
suresh    
maheshwari
Run Code Online (Sandbox Code Playgroud)

样本输出

mahesh    surendra       30
mahesh    mahesh         100
mahesh    shrivatsa      20
mahesh    suresh         60
mahesh    maheshwari     80
suresh    surendra       70
suresh    mahesh         60
suresh    shrivatsa      40
suresh    suresh         100
suresh    maheshwari     30
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 tf-idf 方法匹配这两列的相似性时,我遇到了问题(获取关键错误),因为这些列的长度不同。有没有其他方法可以解决这个问题...任何帮助将不胜感激。我进行了大量搜索,发现在几乎所有情况下,人们都将第一个文档与同一语料库中的其余文档进行比较。这就像将语料库 1 的每个文档与 corpus2 上的每个文档进行比较。

python name-matching dataframe pandas cosine-similarity

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

如何检查特定 pandas 数据框列中的值是否唯一

我需要检查 pandas 数据框列中是否存在多次特定值。这是基本代码;

for index, row in df_x.iterrows():
    try:
        if row[1] in df_y['b'].values:  

# if row[1] exists in df_y i want to know how many time is it repeated, or if it is unique or not

    except Exception as e:
        print('Error ', e)
Run Code Online (Sandbox Code Playgroud)

python unique duplicates dataframe pandas

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

如何将形式 [xmin ymin xmax ymax] 转换为图像中标准化的 [xy 宽度高度]?

我正在使用 Microsoft 的CustomVision.ai构建自定义视觉应用程序。

我正在使用本教程

在对象检测项目中标记图像时,需要使用标准化坐标指定每个标记对象的区域。

我有一个 XML 文件,其中包含有关图像的注释,例如名为sample_1.jpg

<annotation>
        <filename>sample_1.jpg</filename>
    <size>
        <width>410</width>
        <height>400</height>
        <depth>3</depth>
    </size>
    <object>
        <bndbox>
            <xmin>159</xmin>
            <ymin>15</ymin>
            <xmax>396</xmax>
            <ymax>302</ymax>
        </bndbox>
    </object>
</annotation>
Run Code Online (Sandbox Code Playgroud)

我必须根据提供的教程将边界框坐标从 xmin,xmax,ymin,ymax 转换为标准化的 x,y,w,h 坐标。

谁能给我一个转换函数?

python object-detection microsoft-custom-vision

2
推荐指数
2
解决办法
4452
查看次数

如何为散点图上的每个点添加标签?Matplotlib

在这里的第一篇文章。开始在 python 中使用 NFLScrapr 包,并尝试创建一个散点图来显示一些信息。现在散点图只显示点,但我想知道是否有办法从相应的数据中为每个图添加标签?

从这个开始

league_rushing_success = data.loc[(data['play_type']=='run') & (data['down']<=4)].groupby(by='posteam')[['epa','success','yards_gained']].mean()
Run Code Online (Sandbox Code Playgroud)

试图用这个绘图

#Make x and y variables for success rate data
x = league_rushing_success['success'].values
y = league_rushing_success['epa'].values
types = league_rushing_success['posteam'].values

fig, ax = plt.subplots(figsize=(10,10))

#Make a scatter plot with success rate data
ax.scatter(x, y,)

#Adding labels and text
ax.set_xlabel('Rush Success Rate', fontsize=14)
ax.set_ylabel('EPA', fontsize=14)
ax.set_title('Rush Success Rate and EPA', fontsize=18)
ax.text(.46, .39, 'Running Backs Dont Matter', fontsize=10, alpha=.7)

for i,type in enumerate(types):
    x = x_coords[i]
    y = y_coords[i]
    plt.scatter(x, y, …
Run Code Online (Sandbox Code Playgroud)

python scatter matplotlib

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