Home > database >  Python pandas create new dict column from two columns
Python pandas create new dict column from two columns

Time:06-01

I hope you are all doing well. I have a dataframe data looks like that:

import pandas as pd
   data = {'A':  ['A_first_value', 'A_second_value'],
        'B': ['B_first_value', 'B_second_value'],
        'C': ['C_first_value', 'C_second_value'],
        'D': ['D_first_value', 'D_second_value'],
   }
df = pd.DataFrame(data)

Result:

      A               B               C               D
0   A_first_value   B_first_value   C_first_value   D_first_value
1  A_second_value  B_second_value  C_second_value  D_second_value

This should be the target Columns C and D should be in a dict:

#                 A               B                                        Target
# 0   A_first_value   B_first_value  {"C": "C_first_value", "D": "D_first_value"}
# 1  A_second_value  B_second_value  {"C": "C_second_value", "D": "D_second_value"}

I think I would avoid using iterrows because of speed?! Is there an other possibitly?

CodePudding user response:

Use DataFrame.to_dict:

df['Target'] = df[['C','D']].to_dict('records')
df = df.drop(['C','D'], axis=1)
print (df)
                A               B  \
0   A_first_value   B_first_value   
1  A_second_value  B_second_value   

                                           Target  
0    {'C': 'C_first_value', 'D': 'D_first_value'}  
1  {'C': 'C_second_value', 'D': 'D_second_value'}
  • Related