Jin
Jin

Reputation: 1223

Python Pandas data frame with column value as a dictionary

I have a dataframe DF in the following format, first row is the column name

name   val1    val2
 A      1       2
 B      3       4

How to convert to the following data frame format

name    map
 A      {val1:1,val2:2}
 B      {val1:3,val2:4}

Upvotes: 0

Views: 73

Answers (1)

zuku
zuku

Reputation: 667

You can achieve it with to_dict() method of dataframe.

x['map']=x[['val1','val2']].to_dict(orient='records')

Upvotes: 2

Related Questions