Skarl001
Skarl001

Reputation: 112

Create a Dictionary from dataframe with first column as keys and remaining as values

I am trying to create a dictionary that has the elements of the first column of a dataframe as the keys and the remaining columns as values. My dataframe test_df has on its first column the names of the images and the rest contain scores for each image (see code below). The goal is to create a variable that contains a key-value pair which maps the image name (key) to the labels (values).

print(test_df)

    1.jpg   0.21337     0.83585 0.13824     0.16486
0   101.jpg 0.087876    0.86553 0.038267    0.046497
1   106.jpg 0.231350    0.73344 0.038267    0.164860
2   107.jpg 0.406980    0.56384 0.862520    0.164860
3   109.jpg 0.257200    0.25579 0.686770    0.046497

So what I want to do is something like this:

dict = {'1.jpg': 0.21337 0.83585 0.13824 0.16486
        '101.jpg': 0.087876 0.86553 0.038267 0.046497....
        '106.jpg':.......etc}

but the closest thing I got is

print(test_df.transpose().to_dict())

 {0: {'1.jpg': '101.jpg', '0.21337': 0.087876, '0.83585': 0.8655299999999999, '0.13824': 0.038267, '0.16486': 0.046497000000000004}, 1: {'1.jpg': '106.jpg', '0.21337': 0.23135, '0.83585': 0.73344, '0.13824': 0.038267, '0.16486': 0.16485999999999998},...

I also tried this but did not work well

dict = {k: v for k, v in zip(test_df.iloc[:,0], test_df.iloc[0,1:])}

I don't have a lot of experience on dictionaries so I would appreciate any help. Thank you in advance

Upvotes: 2

Views: 4543

Answers (1)

jezrael
jezrael

Reputation: 862511

First add header=None for avoid convert first row of data to columns names.

test_df = pd.read_csv(file, header=None)

print (test_df)
         0         1        2         3         4
0    1.jpg  0.213370  0.83585  0.138240  0.164860
1  101.jpg  0.087876  0.86553  0.038267  0.046497
2  106.jpg  0.231350  0.73344  0.038267  0.164860
3  107.jpg  0.406980  0.56384  0.862520  0.164860
4  109.jpg  0.257200  0.25579  0.686770  0.046497

I think you need dictioanry of lists by DataFrame.set_index for convert first column to index and then use DataFrame.agg for lists:

d = test_df.set_index(0).agg(list, axis=1).to_dict()

print (d)
{'1.jpg': [0.21337, 0.83585, 0.13824, 0.16486], 
 '101.jpg': [0.087876, 0.86553, 0.038267, 0.046497],
 '106.jpg': [0.23135, 0.73344, 0.038267, 0.16486], 
 '107.jpg': [0.40698, 0.56384, 0.86252, 0.16486],
 '109.jpg': [0.2572, 0.25579, 0.68677, 0.046497]}

Or if need join numbers then use:

d1 = test_df.set_index(0).astype(str).agg(' '.join, axis=1).to_dict()

print (d1)
{'1.jpg': '0.21337 0.83585 0.13824 0.16486', 
 '101.jpg': '0.087876 0.86553 0.038267 0.046497',
 '106.jpg': '0.23135 0.73344 0.038267 0.16486', 
 '107.jpg': '0.40698 0.56384 0.86252 0.16486', 
 '109.jpg': '0.2572 0.25579 0.68677 0.046497'}

Alternative solution:

Also is possible convert to index in read_csv by parameter index_col=0:

test_df = pd.read_csv(file, header=None, index_col=0)
d = test_df.agg(list, axis=1).to_dict()

Upvotes: 3

Related Questions