简体   繁体   中英

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}

您可以使用to_dict()方法来实现。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM