我正在使用Python从Googles adwords API中提取数据。我想将这些数据放入Pandas DataFrame中,以便我可以对数据进行分析。我正在使用Google提供的示例here.
下面是我试图将输出作为pandas数据帧读取的尝试:
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格式和错误的数据。
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数据帧。任何帮助将不胜感激。
答案 0 :(得分:6)
所以如果有人好奇或者我遇到同样的问题,我能够找到自己问题的答案。我不得不import io
并将adwords查询的输出写入我命名为output
的字符串。然后我使用seek()
方法从头开始,并使用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()
答案 1 :(得分:0)
您不需要特别将报告下载为AWQL,您可以将其下载为字符串并将其保存在数据框对象中,以便在需要时进行进一步的操作。
import io
import sys
from googleads import adwords
import locale
import sys
import _locale
import pandas as pd
import numpy as np
_locale._getdefaultlocale = (lambda *args: ['en_US', 'UTF-8'])
# The chunk size used for the report download.
CHUNK_SIZE = 16 * 1024
def main(client):
report_downloader = client.GetReportDownloader(version='v201809')
print("here")
# Create report definition.
report = {
'reportName': 'AD_PERFORMANCE_REPORT',
'dateRangeType': 'CUSTOM_DATE',
'reportType': 'AD_PERFORMANCE_REPORT',
'downloadFormat': 'CSV',
'selector': {
'fields': ['AccountDescriptiveName',
'Device',
'Date',
'CampaignName',
'CampaignId',
'AdGroupName',
'Id',
'DisplayUrl',
'LongHeadline',
'HeadlinePart1',
'HeadlinePart2',
'Description',
'Description1',
'Description2',
'Headline',
'Path1',
'Path2',
"BusinessName",
'AdType',
'AccountCurrencyCode',
'Clicks',
'Impressions',
'Ctr',
'AverageCpc',
'AverageCpm',
'Cost'
],
'dateRange': {'min':'20191207','max':'20191217'}
}
}
#date format: yyyymmdd
# print(report)
output = io.StringIO()
# #report_data = open('ad_performance.csv', mode ='w')
report_downloader.DownloadReport(
report, output, skip_report_header=True, skip_column_header=False,
skip_report_summary=True)
output.seek(0)
df = pd.read_csv(output)
print(df)
if __name__ == '__main__':
print("hello")
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client)