I have a dataframe:
Name_ID | URL | Count | Rating
------------------------------------------------
ABC | www.example.com/ABC | 10 | 5
123 | www.example.com/123 | 9 | 4
XYZ | www.example.com/XYZ | 5 | 2
ABC111 | www.example.com/ABC111 | 5 | 2
ABC121 | www.example.com/ABC121 | 5 | 2
222 | www.example.com/222 | 5 | 3
abc222 | www.example.com/abc222 | 4 | 2
ABCaaa | www.example.com/ABCaaa | 4 | 2
I am trying to create a JSON:
{"name": "sampledata",
"children": [
{
"name":9,
"children":[
{"name":4,
"children":[
{"name":"123","size":100}
]
}
]
},
{
"name":10,
"children":[
{"name":5,
"children":[
{"name":"ABC","size":100}
]
}
]
},
{
"name":4,
"children":[
{"name":2,
"children":[
{"name":"abc222","size":50},
{"name":"ABCaaa","size":50}
]
}
]
},
{
"name":5,
"children":[
{"name":2,
"children":[
{"name":"ABC","size":16},
{"name":"ABC111","size":16},
{"name":"ABC121","size":16}
]
},
{"name":3,
"children":[
{"name":"222","size":50}
]
}
]
}
]
}
In order to do that:
I am trying to add labels such as
"name"and"children"to the JSON while creating it.I tried something like
results = [{"name": i, "children": j} for i,j in results.items()]. But it won't label it properly, I believe.Add another field with the label "size" which I am planning to calculate based on the formula
(Rating*Count*10000)/number_of_children_to_the_immediate_parent.
import pandas as pd
from collections import defaultdict
import json
data =[('ABC', 'www.example.com/ABC', 10 , 5), ('123', 'www.example.com/123', 9, 4), ('XYZ', 'www.example.com/XYZ', 5, 2), ('ABC111', 'www.example.com/ABC111', 5, 2), ('ABC121', 'www.example.com/ABC121', 5, 2), ('222', 'www.example.com/222', 5, 3), ('abc222', 'www.example.com/abc222', 4, 2), ('ABCaaa', 'www.example.com/ABCaaa', 4, 2)]
df = pd.DataFrame(data, columns=['Name', 'URL', 'Count', 'Rating'])
gp = df.groupby(['Count'])
dict_json = {"name": "flare"}
children = []
for name, group in gp:
temp = {}
temp["name"] = name
temp["children"] = []
rgp = group.groupby(['Rating'])
for n, g in rgp:
temp2 = {}
temp2["name"] = n
temp2["children"] = g.reset_index().T.to_dict().values()
for t in temp2["children"]:
t["size"] = (t["Rating"] * t["Count"] * 10000) / len(temp2["children"])
t["name"] = t["Name"]
del t["Count"]
del t["Rating"]
del t["URL"]
del t["Name"]
del t["index"]
temp["children"].append(temp2)
children.append(temp)
dict_json["children"] = children
print json.dumps(dict_json, indent=4)
Though the code does print what I need, I am looking for a more efficient and cleaner way to do the same, mainly because the actual dataset might be even more nested and complicated.
'name'identifier at each level? \$\endgroup\$pandasstarting point important? If so, why isn't it a tag? Also this kind of question might be more attention on SO. I know there's a lot more numpy interest there; that's probably true for pandas as well. \$\endgroup\$df.to_json(....)is available? The default doesn't look like what you want, but some combination of parameters might do the job. \$\endgroup\$defaultdictbut I don't see you using it. \$\endgroup\$