I have a DataFrame
which looks like this
df=
Rank Date Age Name Score
3 9001 23 Lilly 40
2 9002 23 Lilly 45
2 8001 19 Tom 80
3 8010 19 Tom 75
1 4040 28 Cindy 85
3 4041 28 Cindy 50
4 3800 37 Don 35
4 3900 38 Don 38
What I am trying to do is to make separate dictionaries
with key
as the "Name"
column and value
field from "Rank, Date, Age and Score"
column values. It should look like this
{ 'Lilly': [3,2] }
{ 'Lilly': [9001,9002] }
{ 'Lilly': 23 }
{ 'Lilly': [40,45] }
------
{ 'Don': [35,38] }
I want use a for loop
instead of the dry code I used, which include a lot of repetitive lines of code
list_1 = df['Rank'].tolist()
list_2 = df['Date'].tolist()
list_3 = df['Age'].tolist()
list_4 = df['Name'].tolist()
list_5 = df['Score'].tolist()
dict_1 = {list_4[i]: list_1[i] for i in range(len(list_1))}
dict_2 = {list_4[i]: list_2[i] for i in range(len(list_1))}
dict_3 = {list_4[i]: list_3[i] for i in range(len(list_1))}
dict_4 = {list_4[i]: list_5[i] for i in range(len(list_1))}
Is it possible to create separate numbered dictionaries
inside a for loop
rather than having to write repetitive lines of code?