简体   繁体   English

熊猫数据帧到有序词典

[英]panda dataframe to ordered dictionary

There is a post in which a panda dataframe is converted in to a dictionary for further processing. 有一个帖子,其中熊猫数据帧被转换为字典以供进一步处理。

The code to do this is: 执行此操作的代码是:

df = pd.read_excel(open('data/file.xlsx', 'rb'), sheetname="Sheet1")
dict = df.set_index('id').T.to_dict('dict')

which yields something like this: {column -> {index -> value}} 产生这样的结果: {column -> {index -> value}}

Is there a quick way to instead of this {column -> {index -> value}} get this: OrderedDict(column, value) as a return value? 有没有快速的方法来代替这个{column -> {index -> value}}得到这个: OrderedDict(column, value)作为返回值?

Currently, I am using the dictionary generated from pandas and assign those values in to an Ordered Dictionary, one by one. 目前,我正在使用从pandas生成的字典,并将这些值逐个分配到有序字典中。 This is not the optimum way, as the order is scrambled 这不是最佳方式,因为订单是乱码的

Example input: An Excel file like this: 示例输入:这样的Excel文件:

Unique_id | column1 | column2 | column3 | column 4
1         | 3       | 4       | 43      | 90
2         | 54      | 6       | 43      | 54

and the output should be an ordered dictionary like this: 输出应该是一个有序的字典,如下所示:

{1:[3,4,43,90], 2:[54,6,43,54]}

You can get the dictionary in the desired order by using an OrderedDict with keys from the Unique_id column. 您可以使用带有Unique_id列中的键的OrderedDict以所需顺序获取字典。 The following should serve as an illustration: 以下内容应作为说明:

from collections import OrderedDict

# Get the unordered dictionary
unordered_dict = df.set_index('Unique_id').T.to_dict('list')

 # Then order it
ordered_dict = OrderedDict((k,unordered_dict.get(k)) for k in df.Unique_id)
# OrderedDict([(1, [3, 4, 43, 90]), (2, [54, 6, 43, 54])])

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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