保存dict为数据帧时如何保持顺序?

2024-10-01 15:47:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一些示例数据:

dic = {'common': {'value': 18, 'attr': 20, 'param': 22},
       'fuzzy': {'value': 14, 'attr': 21, 'param': 24},
       'adhead': {'value': 13, 'attr': 20, 'param': 29}}

执行pd.DataFrame(dic)我得到:

       common  fuzzy  adhead
attr       20     21      20
param      22     24      29
value      18     14      13

在这里,'外部'列是可以的,但'内部'排序,这是我需要避免的。如何快速完成?(需要保持排序-此处对行进行排序)

提示:colsole上有个消息:

main:95: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=True'.

To retain the current behavior and silence the warning, pass sort=False

但不知道是为了什么。将此参数传递给pd.DataFrame(dic, sort=False)返回: TypeError: __init__() got an unexpected keyword argument 'sort'。你知道吗


Tags: thetodataframeparam排序valuenotfuture

热门问题