0

I have dataframe like below

A B C
1 2 3
4 5 6 
7 8 9
0 1 2
...

I need a new column D like below:

A B C D
1 2 3 {'A':1,'B':2,'C':3}
4 5 6 {'A':4,'B':5,'C':6}
7 8 9 {'A':7,'B':8,'C':9}
0 1 2 {'A':0,'B':1,'C':2}
  • What have you tried so far? Did you check the `pd.to_dict()` function? – Mig B Dec 09 '20 at 14:51
  • Does this answer your question? [python pandas dataframe to dictionary](https://stackoverflow.com/questions/18695605/python-pandas-dataframe-to-dictionary) – Mig B Dec 09 '20 at 14:52

1 Answers1

0

With to_dict

df['D'] = df.to_dict('records')
df
Out[11]: 
   A  B  C                         D
0  1  2  3  {'A': 1, 'B': 2, 'C': 3}
1  4  5  6  {'A': 4, 'B': 5, 'C': 6}
2  7  8  9  {'A': 7, 'B': 8, 'C': 9}
3  0  1  2  {'A': 0, 'B': 1, 'C': 2}
BENY
  • 317,841
  • 20
  • 164
  • 234