'Converting a dataframe to a dictionary with tuples as values

I am trying to convert a Pandas DataFrame to a dictionary. I would like to have pid be the key and the remaining two columns be values within the tuples.

I have tried aggregated_events.set_index('pid').to_dict('list') and aggregated_events.set_index('pid').to_dict() but know I am missing something. Any help would be greatly appreciated!

Original Dataframe



Solution 1:[1]

You can first transpose your dataframe to get first column as new column names, something like this:

df = df.set_index('pid').T

Then you can use to_dict to convert a dataframe to a dictionary.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1