Skip to content
Advertisement

Pandas DataFrame Groupby two columns and get different relation in same keys insert list

I have this table :

JavaScript

I have to create a dictionary with Head key and values ​​equal to the relations but not repeated and for each value of the relations I have to insert the corresponding tail.

example:

JavaScript

I don’t really know how to do it. Is there someone who can help me?

Result with nan.

Second Example Input:

JavaScript

the output:

JavaScript

I would like to remove the nans

Advertisement

Answer

You could use pivot_table and to_dict:

JavaScript

Or, the other way around:

JavaScript

output:

JavaScript
post-processing the output to remove NaNs:
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement