Let’s say I have the DataFrame
JavaScript
x
5
1
filename size inverse similarity
2
123.txt 1 2 34
3
323.txt 3 1 44
4
222.txt 4 1 43
5
I want to create a dictionary in the form
JavaScript
1
4
1
{'123.txt': [1, 2, 34],
2
'323.txt': [3, 1, 44],
3
'222.txt': [4, 1, 43]}
4
Solutions I have found deal with the case of creating a dict with single values using something like
JavaScript
1
2
1
df.set_index('Filename')['size'].to_dict()
2
Advertisement
Answer
Set 'filename'
as the index, take the transpose, then use to_dict
with orient='list'
:
JavaScript
1
2
1
my_dict = df.set_index('filename').T.to_dict(orient='list')
2
The resulting output:
JavaScript
1
2
1
{'323.txt': [3, 1, 44], '222.txt': [4, 1, 43], '123.txt': [1, 2, 34]}
2