0

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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Welcome to Stack Overflow! Please take the [tour]. You have duplicate pid's, so what are you expecting the output to be? Please [edit] to add it. As well, please provide the input df as text, [not a picture](https://meta.stackoverflow.com/q/285551/4518341), and include the bad output too, for completeness. See [How to make good reproducible pandas examples](/q/20109391/4518341), [mre], and [ask]. – wjandrea Feb 01 '22 at 22:00

1 Answers1

0

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.

Anynamer
  • 334
  • 1
  • 6