0

I would like to get for each category/eye colour to have all the associated first names.

Here is my dataframe (df):

      eye color           first name 
0     blue                Jules
1     blue                Lucie
2     green               Thomas
3     green               Vincent
4     green               David
5     brown               Maxime

This is the output I would like to have:

{'blue': ['Jules', 'Lucie'], 'green': ['Thomas', 'Vincent', 'David'], 'brown': ['Maxime']

This is my code:

list_name=list()

for i in range(len(df)-1):
    
    current_color=df['eye color'][i]
    
    next_color=df['eye color'][i+1] 
    
    name=df['first name'][i]
    
    if current_color!=next_color : 

        compte_nb_systeme=compte_nb_systeme+1        
        print('we change eye color')
    else :
        print('we don't change the color of the eye')
        list_name.append(name)
        
   dico= {current_color :list_name}            
print(dico) 

the problem is that I add all the names contained in the column 'first name' and that for each color of eyes.

mpx
  • 3,081
  • 2
  • 26
  • 56
lmj
  • 1
  • 3

1 Answers1

0

This code iterates over all rows of df and adds the first name to the dict entry of the eye color. For the first occurrence of a color (color not in output) the entry in the dict must be created, it is initialized to an empty list (output[color] = []).

output = {}

for i in range(len(df)):

    color = df['eye color'][i]
    first_name = df['first name'][i]

    if color not in output:
        output[color] = []
    output[color].append(first_name)
    
print(output)