Google Analytics(分析)数据到Pandas数据框

阿比·辛格

我正在尝试使用Google Analytics(分析)API将Google Analytics(分析)数据发送到熊猫数据框。我遵循了官方文档中提供的代码示例,现在有了可以打印出所需数据的代码。我需要帮助弄清楚如何将数据发送到pandas数据框,而不仅仅是打印出来。

一旦执行查询,这就是我得到的原始输出:

{'kind': 'analytics#gaData', 'id': 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXXXXX&dimensions=ga:date&metrics=ga:sessions,ga:transactions&start-date=7daysAgo&end-date=today', 'query': {'start-date': '7daysAgo', 'end-date': 'today', 'ids': 'ga:XXXXXXX', 'dimensions': 'ga:date', 'metrics': ['ga:sessions', 'ga:transactions'], 'start-index': 1, 'max-results': 1000}, 'itemsPerPage': 1000, 'totalResults': 8, 'selfLink': 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXXXXX&dimensions=ga:date&metrics=ga:sessions,ga:transactions&start-date=7daysAgo&end-date=today', 'profileInfo': {'profileId': 'XXXXXXX', 'accountId': 'XXXXXXX', 'webPropertyId': 'XXXXXXX', 'internalWebPropertyId': 'XXXXXXX', 'profileName': 'XXXXXXX', 'tableId': 'ga:XXXXXXX'}, 'containsSampledData': False, 'columnHeaders': [{'name': 'ga:date', 'columnType': 'DIMENSION', 'dataType': 'STRING'}, {'name': 'ga:sessions', 'columnType': 'METRIC', 'dataType': 'INTEGER'}, {'name': 'ga:transactions', 'columnType': 'METRIC', 'dataType': 'INTEGER'}], 'totalsForAllResults': {'ga:sessions': '86913', 'ga:transactions': '312'}, 'rows': [['20200114', '11965', '41'], ['20200115', '11052', '51'], ['20200116', '11396', '38'], ['20200117', '11097', '28'], ['20200118', '10490', '46'], ['20200119', '9829', '34'], ['20200120', '12280', '36'], ['20200121', '8804', '38']]}

Google文档使用此功能在打印语句中输出此数据:

def print_results(results):

    # Print header.
    output = []
    for header in results.get('columnHeaders'):
        output.append('%30s' % header.get('name'))
    print(''.join(output))

    # Print data table.
    if results.get('rows', []):
        for row in results.get('rows'):
            output = []
            for cell in row:
                output.append('%30s' % cell)
            print(''.join(output))
    else:
        print('No Rows Found')

如您所见,我们需要捕获results[columnHeaders][name]为列标题,并且需要捕获results[rows]为需要馈入熊猫数据帧的数据。

如何创建将这些数据放入数据框的函数?

Subhrajyoti das

试试下面的代码:

import pandas as pd
results = {'kind': 'analytics#gaData', 'id': 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXXXXX&dimensions=ga:date&metrics=ga:sessions,ga:transactions&start-date=7daysAgo&end-date=today', 'query': {'start-date': '7daysAgo', 'end-date': 'today', 'ids': 'ga:XXXXXXX', 'dimensions': 'ga:date', 'metrics': ['ga:sessions', 'ga:transactions'], 'start-index': 1, 'max-results': 1000}, 'itemsPerPage': 1000, 'totalResults': 8, 'selfLink': 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXXXXX&dimensions=ga:date&metrics=ga:sessions,ga:transactions&start-date=7daysAgo&end-date=today', 'profileInfo': {'profileId': 'XXXXXXX', 'accountId': 'XXXXXXX', 'webPropertyId': 'XXXXXXX', 'internalWebPropertyId': 'XXXXXXX', 'profileName': 'XXXXXXX', 'tableId': 'ga:XXXXXXX'}, 'containsSampledData': False, 'columnHeaders': [{'name': 'ga:date', 'columnType': 'DIMENSION', 'dataType': 'STRING'}, {'name': 'ga:sessions', 'columnType': 'METRIC', 'dataType': 'INTEGER'}, {'name': 'ga:transactions', 'columnType': 'METRIC', 'dataType': 'INTEGER'}], 'totalsForAllResults': {'ga:sessions': '86913', 'ga:transactions': '312'}, 'rows': [['20200114', '11965', '41'], ['20200115', '11052', '51'], ['20200116', '11396', '38'], ['20200117', '11097', '28'], ['20200118', '10490', '46'], ['20200119', '9829', '34'], ['20200120', '12280', '36'], ['20200121', '8804', '38']]}

def print_results(results):
    column_names = []
    for header in results.get('columnHeaders'):
        column_names.append(header.get('name'))
    data = results.get('rows')
    create_dataframe(data, column_names)

def create_dataframe(data, column_names):
    df = pd.DataFrame(data, columns = column_names)
    #prints the dataframe
    print(df)

print_results(results)

#output
    ga:date ga:sessions ga:transactions
0  20200114       11965              41
1  20200115       11052              51
2  20200116       11396              38
3  20200117       11097              28
4  20200118       10490              46
5  20200119        9829              34
6  20200120       12280              36
7  20200121        8804              38


本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Google Analytics(分析)中获取数据

来自分类常见问题

将数据推送到Google Analytics(分析)

来自分类Dev

R脚本从Google Analytics(分析)导入数据

来自分类Dev

Google Analytics(分析)API不返回数据

来自分类Dev

将数据推送到Google Analytics(分析)

来自分类Dev

Google Analytics(分析)数据集类型(CRMint)

来自分类Dev

从Python API访问Google Analytics(分析)数据

来自分类Dev

Google Analytics(分析)跟踪动态数据

来自分类Dev

Google Analytics(分析)数据处理时区

来自分类Dev

Google Analytics(分析)API不返回数据

来自分类Dev

Google Analytics(分析)未跟踪数据

来自分类Dev

R脚本从Google Analytics(分析)导入数据

来自分类Dev

获取每个用户的Google Analytics(分析)数据

来自分类Dev

Google Analytics(分析)行为流数据导出?

来自分类Dev

Google Analytics(分析)Cookie数据从Webform进入数据库

来自分类Dev

为什么我从Google Analytics(分析)API获取的数据与Google Analytics(分析)网络界面不匹配?

来自分类Dev

如何从Google Analytics(分析)API获取Adwords广告系列数据?

来自分类Dev

节点:通过服务帐户的Google Analytics(分析)数据

来自分类Dev

Rails,从Google Analytics(分析)中获取特定屏幕的数据

来自分类Dev

仅使用API密钥访问Google Analytics(分析)数据

来自分类Dev

在我的网站上显示Google Analytics(分析)数据?

来自分类Dev

如何导出完整的Google Analytics(分析)历史数据?

来自分类Dev

无法使用RGA R库从Google Analytics(分析)获取数据

来自分类Dev

无法从CloudConnect中的Google Analytics(分析)下载器获取数据

来自分类Dev

如何在Google Analytics(分析)上查看年度数据?

来自分类Dev

iOS Google Analytics(分析)-没有数据通过

来自分类Dev

如何在我的页面中集成Google Analytics(分析)数据?

来自分类Dev

如何获取访问令牌以获取Google Analytics(分析)数据?

来自分类Dev

要将事件数据发送到Google Analytics(分析)?

Related 相关文章

  1. 1

    从Google Analytics(分析)中获取数据

  2. 2

    将数据推送到Google Analytics(分析)

  3. 3

    R脚本从Google Analytics(分析)导入数据

  4. 4

    Google Analytics(分析)API不返回数据

  5. 5

    将数据推送到Google Analytics(分析)

  6. 6

    Google Analytics(分析)数据集类型(CRMint)

  7. 7

    从Python API访问Google Analytics(分析)数据

  8. 8

    Google Analytics(分析)跟踪动态数据

  9. 9

    Google Analytics(分析)数据处理时区

  10. 10

    Google Analytics(分析)API不返回数据

  11. 11

    Google Analytics(分析)未跟踪数据

  12. 12

    R脚本从Google Analytics(分析)导入数据

  13. 13

    获取每个用户的Google Analytics(分析)数据

  14. 14

    Google Analytics(分析)行为流数据导出?

  15. 15

    Google Analytics(分析)Cookie数据从Webform进入数据库

  16. 16

    为什么我从Google Analytics(分析)API获取的数据与Google Analytics(分析)网络界面不匹配?

  17. 17

    如何从Google Analytics(分析)API获取Adwords广告系列数据?

  18. 18

    节点:通过服务帐户的Google Analytics(分析)数据

  19. 19

    Rails,从Google Analytics(分析)中获取特定屏幕的数据

  20. 20

    仅使用API密钥访问Google Analytics(分析)数据

  21. 21

    在我的网站上显示Google Analytics(分析)数据?

  22. 22

    如何导出完整的Google Analytics(分析)历史数据?

  23. 23

    无法使用RGA R库从Google Analytics(分析)获取数据

  24. 24

    无法从CloudConnect中的Google Analytics(分析)下载器获取数据

  25. 25

    如何在Google Analytics(分析)上查看年度数据?

  26. 26

    iOS Google Analytics(分析)-没有数据通过

  27. 27

    如何在我的页面中集成Google Analytics(分析)数据?

  28. 28

    如何获取访问令牌以获取Google Analytics(分析)数据?

  29. 29

    要将事件数据发送到Google Analytics(分析)?

热门标签

归档