I am trying to convert a simple pandas dataframe into a nested JSON file based on the answer I found here: pandas groupby to nested json
My grouped dataframe looks like this:
firstname lastname orgname phone mobile email
teamname members
1 0 John Doe Anon 916-555-1234 none [email protected]
1 Jane Doe Anon 916-555-4321 916-555-7890 [email protected]
2 0 Mickey Moose Moosers 916-555-0000 916-555-1111 [email protected]
1 Minny Moose Moosers 916-555-2222 none [email protected]
My code is:
data = pandas.read_excel(inputExcel, sheetname = 'Sheet1', encoding = 'utf8')
grouped = data.groupby(['teamname', 'members']).first()
results = defaultdict(lambda: defaultdict(dict))
for index, value in grouped.itertuples():
for i, key in enumerate(index):
if i ==0:
nested = results[key]
elif i == len(index) -1:
nested[key] = value
else:
nested = nested[key]
print json.dumps(results, indent = 4)
I get the following error on the first "for" loop. What causes this error in this circumstance and what would it take to fix it to output the nested json?
for index, value in grouped.itertuples():
ValueError: too many values to unpack