I am trying to extract some data from the json data frame below, using Python 2.7.9.
I am trying to get to the counts in a single row that are listed inside the "steps" list using the code below:
data2 = (json.loads(data));
for data_key,data_value in data2['data'].items():
for date_key,date_value in data_value.items():
for steps_value in date_value:
if 'steps' == date_key: #looping on dictionary object
print data_key,steps_value['count'],steps_value['event'],steps_value['custom_event']
On digging further, I managed to make some progress and get to the values. I am not a Python expert, so wondering if this could be done in a more elegant manner.Still looking to get to the output as below.
Ideal Output format:
Date1 Step1 Count1 Step2 Count2
Date2 Step2 Count1 Step2 Count2
Sample Data frame:
"frame":
{"dates": ["2016-09-12", "2016-09-19", "2016-09-26"]},
"data": {
"2016-09-12":
{"steps": [
{"count": 325788, "step_conv_ratio": 1, "goal": "App Open", "overall_conv_ratio": 1, "avg_time": null, "event": "App Open"},
{"count": 20524, "step_conv_ratio": 0.627875673029858, "goal": "Game Played",
"avg_time": 572, "event": "Game Played"}],
"analysis": {"completion": 20524, "starting_amount": 32688, "steps": 2, "worst": 1}},
"2016-09-19":
{"steps": [
{"count": 32186, "step_conv_ratio": 1, "goal": "App Open", "overall_conv_ratio": 1, "avg_time": null, "event": "App Open"},
{"count": 20809, "step_conv_ratio": 0.6405528535369082, "goal": "Game Played",
"avg_time": 698, "event": "Game Played"}],
"analysis": {"completion": 20809, "starting_amount": 32486, "steps": 2, "worst": 1}},
"2016-09-26":
{"steps": [
{"count": 456, "step_conv_ratio": 1, "goal": "App Open", "overall_conv_ratio": 1, "avg_time": null, "event": "App Open"},
{"count": 587, "step_conv_ratio": 0.7873688132646091, "goal": "Game Played",
"avg_time": 571, "event": "Game Played"}],
"analysis": {"completion": 12679, "starting_amount": 16103, "steps": 2, "worst": 1}}
}}
Any help / suggestions would be appreciated.
Thanks
If every date only has two steps you could do something like:
stuff = json.loads(data)
dates = stuff["frame"]["dates"] # list of dates as strings
for date in dates:
steps = stuff["data"][date]["steps"] # list of dicts
print("{0}: step 1: {1}, step 2: {2}".format(date, steps[0]["count"], steps[1]["count"]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.