0

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?

Itzik
  • 3
  • 1
sebin
  • 63
  • 3
  • The short answer is no. You cannot create new variable names inside of a loop. The way around this is to use a dictionary to store the dictionaries. – James Dec 07 '20 at 09:06

3 Answers3

0

In general I would recommend storing them not separate, but rather in a dictionary itself. That's how variable assignment in a python for loop is made How do you create different variable names while in a loop?.

Then I would use a simple for loop that iterates through your df columns:

for column in df:
    print(df[column])
Merk
  • 171
  • 12
0

If you like to create something like this

[{'Lilly': 2, 'Tom': 3, 'Cindy': 3, 'Don': 4},
 {'Lilly': 9002, 'Tom': 8010, 'Cindy': 4041, 'Don': 3900},
 {'Lilly': 23, 'Tom': 19, 'Cindy': 28, 'Don': 38},
 {'Lilly': 45, 'Tom': 75, 'Cindy': 50, 'Don': 38}]

with for loop you can use this code block under your dataframe instead of these repetitive ones:

list_of_dicts=[]
name_list = df['Name'].tolist()
for key in df.keys():
    if key == 'Name': continue
    else:
        templist = df[key].tolist()
        tempdict = {name_list[i]: templist[i] for i in range(len(list_1))}
        list_of_dicts.append(tempdict)
list_of_dicts
baymurat
  • 81
  • 5
0

Try this:

groups = df.groupby('Name')
lst = []
for c in df.columns:
    if not c=='Name': lst = lst + [{k:list(v.values())} for k,v in 
        pd.DataFrame(groups[c].apply(list)).to_dict('index').items()]
Itzik
  • 3
  • 1