Still a newbie to Python so please be gentle.
I'm trying to parse a Google Analytics Reporting API V4 response to a Pandas dataframe in Python, specifically using Repl if that makes any difference. I've tried a few commonly accepted answers from SO but none seem to be working. The code 'successfully' (no error msgs) executes but even basic print('Done') commands at the end of the script aren't showing up in the terminal.
Can anyone see what might be wrong with my code?
import pandas as pd
from pandas import json_normalize
response = {'reports':[{'columnHeader':{'dimensions':['ga:date','ga:sourceMedium','ga:landingPagePath','ga:deviceCategory','ga:browser'],'metricHeader':{'metricHeaderEntries':[{'name':'ga:sessions','type':'INTEGER'}]}},'data':{'rows':[{'dimensions':['20201126','(direct) / (none)','/test/page.html','desktop','Chrome'],'metrics':[{'values':['1']}]}],'totals':[{'values':['1000']}],'rowCount':100,'minimums':[{'values':['1']}],'maximums':[{'values':['10']}],'isDataGolden':True},'nextPageToken':'1'}]}
def parse_data(response):
reports = response['reports'][0]
columnHeader = reports['columnHeader']['dimensions']
metricHeader = reports['columnHeader']['metricHeader']['metricHeaderEntries']
columns = columnHeader
for metric in metricHeader:
columns.append(metric['name'])
data = pd.json_normalize(reports['data']['rows'])
data_dimensions = pd.DataFrame(data['dimensions'].tolist())
data_metrics = pd.DataFrame(data['metrics'].tolist())
data_metrics = data_metrics.applymap(lambda x: x['values'])
data_metrics = pd.DataFrame(data_metrics[0].tolist())
result = pd.concat([data_dimensions, data_metrics], axis=1, ignore_index=True)
return result
print(result)
result.to_csv('result.csv')
print('Done')