gpt4 book ai didi

python - 将函数返回的键值作为新列附加到 Dataframe

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:27 26 4
gpt4 key购买 nike

我有一个包含 URL 列表的数据框,我想为其提取几个值。然后,应将返回的键/值添加到原始数据帧,并将键作为新列和相应的值。

我认为这会神奇地发生在result_type='expand' 显然不是。当我尝试时

df5["data"] = df5.apply(lambda x: request_function(x['url']),axis=1, result_type='expand')

我最终将结果全部集中在一个数据列中:

[{'title': ['Python Notebooks: Connect to Google Search Console API and Extract Data - Adapt'], 'description': []}]

我的目标结果是一个包含以下 3 列的数据框:

| URL|      Title      |  Description|

这是我的代码:

import requests
from requests_html import HTMLSession
import pandas as pd
from urllib import parse

ex_dic = {'url': ['https://www.searchenginejournal.com/reorganizing-xml-sitemaps-python/295539/', 'https://searchengineland.com/check-urls-indexed-google-using-python-259773', 'https://adaptpartners.com/technical-seo/python-notebooks-connect-to-google-search-console-api-and-extract-data/']}

df5 = pd.DataFrame(ex_dic)
df5

def request_function(url):
try:
found_results = []
r = session.get(url)
title = r.html.xpath('//title/text()')
description = r.html.xpath("//meta[@name='description']/@content")
found_results.append({ 'title': title, 'description': description})
return found_results


except requests.RequestException:
print("Connectivity error")
except (KeyError):
print("anoter error")

df5.apply(lambda x: request_function(x['url']),axis=1, result_type='expand')

最佳答案

ex_dic 应该是字典列表,以便您可以更新应用的属性。

import requests
from requests_html import HTMLSession
import pandas as pd
from urllib import parse

ex_dic = {'url': ['https://www.searchenginejournal.com/reorganizing-xml-sitemaps-python/295539/', 'https://searchengineland.com/check-urls-indexed-google-using-python-259773', 'https://adaptpartners.com/technical-seo/python-notebooks-connect-to-google-search-console-api-and-extract-data/']}

ex_dic['url'] = [{'url': item} for item in ex_dic['url']]

df5 = pd.DataFrame(ex_dic)
session = HTMLSession()

def request_function(url):
try:
print(url)
r = session.get(url['url'])
title = r.html.xpath('//title/text()')
description = r.html.xpath("//meta[@name='description']/@content")
url.update({ 'title': title, 'description': description})
return url


except requests.RequestException:
print("Connectivity error")
except (KeyError):
print("anoter error")

df6 = df5.apply(lambda x: request_function(x['url']),axis=1, result_type='expand')
print df6

关于python - 将函数返回的键值作为新列附加到 Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com