如何从 Adwords API 中提取数据并放入 Pandas 数据框 [英] How To Pull Data From the Adwords API and put into a Pandas Dataframe

查看:32
本文介绍了如何从 Adwords API 中提取数据并放入 Pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 从 Google 的 Adwords API 中提取数据.我想将该数据放入 Pandas DataFrame 中,以便对数据进行分析.我正在使用 Google 提供的示例 在这里.

I'm using Python to pull data from Googles adwords API. I'd like to put that data into a Pandas DataFrame so I can perform analysis on the data. I'm using examples provided by Google here.

以下是我尝试将输出读取为熊猫数据框的尝试:

Below is my attempt to try to get the output to be read as a pandas dataframe:

from googleads import adwords
import pandas as pd
import numpy as np

# Initialize appropriate service.

adwords_client = adwords.AdWordsClient.LoadFromStorage()

report_downloader = adwords_client.GetReportDownloader(version='v201710')

# Create report query.
report_query = ('''
select Date, Clicks
from ACCOUNT_PERFORMANCE_REPORT
during LAST_7_DAYS''')

df = pd.read_csv(report_downloader.DownloadReportWithAwql(
    report_query, 
    'CSV',
    client_customer_id='xxx-xxx-xxxx', # denotes which adw account to pull from
    skip_report_header=True, 
    skip_column_header=False,
    skip_report_summary=True, 
    include_zero_impressions=True))

输出是类似于 csv 格式的数据和一个错误.

The output is the data in what looks like csv format and an error.

Day,Clicks
2017-11-05,42061
2017-11-07,45792
2017-11-03,36874
2017-11-02,39790
2017-11-06,44934
2017-11-08,45631
2017-11-04,36031
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-cc25e32c9f3a> in <module>()
     25     skip_column_header=False,
     26     skip_report_summary=True,
---> 27     include_zero_impressions=True))

/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    653                     skip_blank_lines=skip_blank_lines)
    654 
--> 655         return _read(filepath_or_buffer, kwds)
    656 
    657     parser_f.__name__ = name

/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    390     compression = _infer_compression(filepath_or_buffer, compression)
    391     filepath_or_buffer, _, compression = get_filepath_or_buffer(
--> 392         filepath_or_buffer, encoding, compression)
    393     kwds['compression'] = compression
    394 

/anaconda/lib/python3.6/site-packages/pandas/io/common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression)
    208     if not is_file_like(filepath_or_buffer):
    209         msg = "Invalid file path or buffer object type: {_type}"
--> 210         raise ValueError(msg.format(_type=type(filepath_or_buffer)))
    211 
    212     return filepath_or_buffer, None, compression

ValueError: Invalid file path or buffer object type: <class 'NoneType'>

我知道我遗漏了一些基本的东西,而且我不完全了解如何将数据放入 Pandas 数据框.任何帮助将不胜感激.

I know I am missing something fundamental and I do not fully understand how to get data into a pandas dataframe. Any help would be greatly appreciated.

推荐答案

因此,如果有人好奇或遇到与我相同的问题,我可以找到我自己问题的答案.我必须import io 并将adwords 查询的输出写入我命名为output 的字符串.然后我使用 seek() 方法从头开始并使用 pandas read_csv 读取.

So I was able to find out the answer to my own question if anybody is curious or had the same problem I did. I had to import io and wrote the output from the adwords query to a string that I named output. I then used the seek() method to start from the beginning and read that using pandas read_csv.

from googleads import adwords
import pandas as pd
import numpy as np
import io

# Define output as a string
output = io.StringIO()

# Initialize appropriate service.
adwords_client = adwords.AdWordsClient.LoadFromStorage()

report_downloader = adwords_client.GetReportDownloader(version='v201710')

# Create report query.
report_query = ('''
select Date, HourOfDay, Clicks
from ACCOUNT_PERFORMANCE_REPORT
during LAST_7_DAYS''')

# Write query result to output file
report_downloader.DownloadReportWithAwql(
    report_query, 
    'CSV',
    output,
    client_customer_id='xxx-xxx-xxx', # denotes which adw account to pull from
    skip_report_header=True, 
    skip_column_header=False,
    skip_report_summary=True, 
    include_zero_impressions=False)


output.seek(0)

df = pd.read_csv(output)

df.head()

这篇关于如何从 Adwords API 中提取数据并放入 Pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆