DataFrame.to_dict(orient=’dict’)详解编程语言

DataFrame.to_dict(orient=’dict’)

>>> df = pd.DataFrame({'name':[1,2,3],"class":[11,22,33],"price":[111,222,333]}) 
>>> df 
   class  name  price 
0     11     1    111 
1     22     2    222 
2     33     3    333

orient : str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}
Determines the type of the values of the dictionary.

dict (default) : dict like {column -> {index -> value}}

>>> df.to_dict(orient="dict") 
{'class': {0: 11, 1: 22, 2: 33}, 'name': {0: 1, 1: 2, 2: 3}, 'price': {0: 111, 1: 222, 2: 333}}

list : dict like {column -> [values]}

>>> df.to_dict(orient="list") 
{'class': [11, 22, 33], 'name': [1, 2, 3], 'price': [111, 222, 333]}

series : dict like {column -> Series(values)}

>>> df.to_dict(orient="series") 
{'class': 0    11 
1    22 
2    33 
Name: class, dtype: int64, 'name': 0    1 
1    2 
2    3 
Name: name, dtype: int64, 'price': 0    111 
1    222 
2    333 
Name: price, dtype: int64}

split : dict like {index -> [index], columns -> [columns], data -> [values]}

>>> df.to_dict(orient="split") 
{'index': [0, 1, 2], 'columns': ['class', 'name', 'price'], 'data': [[11, 1, 111], [22, 2, 222], [33, 3, 333]]}

records : list like [{column -> value}, … , {column -> value}]

>>> df.to_dict(orient="records") 
[{'class': 11, 'name': 1, 'price': 111}, {'class': 22, 'name': 2, 'price': 222}, {'class': 33, 'name': 3, 'price': 333}]

index : dict like {index -> {column -> value}}

>>> df.to_dict(orient="index") 
{0: {'class': 11, 'name': 1, 'price': 111}, 1: {'class': 22, 'name': 2, 'price': 222}, 2: {'class': 33, 'name': 3, 'price': 333}}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17083.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论