I have a dataframe from which the columns are grouped as:
{(a, b, c): [('d', e, f)]}
with this command:
dct = df.groupby(['a','b','c'])[['d','e','f']].apply(
lambda g: list(map(tuple, g.values.tolist()))).to_dict()
After this, I apply:
dct = {k: dict(v) for k,v in dct.items()}
which gives me the following error:
ValueError: dictionary update sequence element #0 has length 3; 2 is required
I want to make a dictionary in following format.
{(a,b,c):{d:(e,f)}}
CodePudding user response:
Is this what you mean?
dct = {k: {v[0][0]:v[0][1:]} for k,v in dct.items()}