0

I have pandas dataframe that contain one column that its values is dictionary. now I need to enter the keys as column and the values into the correct cells.

df_initial =      a     b    c                            kind
             0  0.50  bibi   23    {'d': 11, 'g': 0.8, 'r': 45}
             1  0.80  cici  140    {'d': 18, 'g': 0.1, 'r': 75}
             2  0.01  didi  320  {'d': 101, 'g': 0.05, 'r': 32}
             3  0.12  mimi    3   {'d': 41, 'g': 0.26, 'r': 64}

the desired results dataframe:

df_final =       a     b    c                            kind    d     g   r
            0  0.50  bibi   23    {'d': 11, 'g': 0.8, 'r': 45}   11  0.80  45
            1  0.80  cici  140    {'d': 18, 'g': 0.1, 'r': 75}   18  0.10  75
            2  0.01  didi  320  {'d': 101, 'g': 0.05, 'r': 32}  101  0.05  32
            3  0.12  mimi    3   {'d': 41, 'g': 0.26, 'r': 64}   41  0.26  64
user18628648
  • 101
  • 5
  • df_initial['d'] = df_initial['kind'].apply(lambda dict_initial: dict_initial['d']) df_initial['g'] = df_initial['kind'].apply(lambda dict_initial: dict_initial['g']) df_initial['r'] = df_initial['kind'].apply(lambda dict_initial: dict_initial['r']) df_initial – Ian May 29 '22 at 13:46

0 Answers0