Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python pandas create new dict column from two columns

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#                 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?

>Solution :

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'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading