gpt4 book ai didi

python - 在 Pandas Dataframe 上迭代 Google 分析 API 函数

转载 作者:行者123 更新时间:2023-12-02 07:30:15 25 4
gpt4 key购买 nike

我有一个包含输入的 Dataframe (f):页面标题 (title_)、开始日期 ('date_') 和结束日期 (date_date),并且我想在 DataFrame 中的每一行上迭代 Google Analytics API get_report 函数,获取结果的新 DataFrame。

这是我的代码:

for i in f.index:
lista = []
df = print_response(get_report(
config.service,
start_date=[f.loc[i]['date_']],
end_date=[f.loc[i]['date_date']],
view_id='xxxxxxxx',
metrics=[{'expression': 'ga:pageViews'}],
dimensionFilterClauses= [{
"filters": [{
"dimensionName": "ga:pageTitle",
"expressions": [f.loc[i]['title_']]}]
}],
dimensions=[{"name": "ga:pageTitle"}]))
lista.append(df)
time.sleep(2)
print(lista)

我收到错误:

TypeError: Object of type 'date' is not JSON serializable

我的代码有什么问题,如何将输出构建为 DataFrame?

[更新]这是我的 get_report 函数:

def get_report(analytics, start_date, end_date, view_id, metrics, dimensions, dimensionFilterClauses):
return analytics.reports().batchGet(
body={
"reportRequests":[
{
"viewId": view_id,
"dateRanges":[{"startDate":start_date,"endDate":end_date}],
"metrics":metrics,
"dimensions": dimensions,
"dimensionFilterClauses": dimensionFilterClauses,
}]
}).execute()

最佳答案

您是否尝试过遵循 quick start

def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.

Args:
response: An Analytics Reporting API V4 response.
"""
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])

for header, dimension in zip(dimensionHeaders, dimensions):
print header + ': ' + dimension

for i, values in enumerate(dateRangeValues):
print 'Date range: ' + str(i)
for metricHeader, value in zip(metricHeaders, values.get('values')):
print metricHeader.get('name') + ': ' + value

关于python - 在 Pandas Dataframe 上迭代 Google 分析 API 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48461367/

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