Skip to content
Advertisement

Convert pandas DataFrame to dict where each value is a list of values of multiple columns

Let’s say I have the DataFrame

JavaScript

I want to create a dictionary in the form

JavaScript

Solutions I have found deal with the case of creating a dict with single values using something like

JavaScript

Advertisement

Answer

Set 'filename' as the index, take the transpose, then use to_dict with orient='list':

JavaScript

The resulting output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement