How to solve the value error received while working with this dictionary?

How to solve the value error received while working with this dictionary?

Problem Description:

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)}}

Solution – 1

Is this what you mean?

dct = {k: {v[0][0]:v[0][1:]} for k,v in dct.items()} 

 
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject