Python 从ordereddict生成pandas数据框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44365209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate a pandas dataframe from ordereddict?
提问by E. Muuli
I am trying to create a pandas dataframe from an ordereddict to preserve the order of the values. But for some reason after creating the dataframe the fields are messed up again.
我正在尝试从一个有序的字典创建一个 Pandas 数据框以保留值的顺序。但是由于某种原因,在创建数据框后,字段再次混乱。
Here's the list of ordereddicts:
这是有序字典的列表:
[OrderedDict([
('key_a',
'value_a'),
('key_b',
'value_b'),
]),
OrderedDict([
('key_a',
'value_c'),
('key_b',
'value_d'),
])
]
Now how should I create a pandas DataFrame from these? What I am looking for is something like that (the important thing is the key_a and key_b etc column name order):
现在我应该如何从这些创建一个 Pandas DataFrame?我正在寻找的是类似的东西(重要的是 key_a 和 key_b 等列名顺序):
key_a key_b
0 value_a value_b
1 value_c value_d
I have tried:
我试过了:
pd.DataFrame.from_records(orderedDictList)
pd.DataFrame.from_dict(orderedDictList)
Feel free to ask any additional questions.
随时提出任何其他问题。
回答by Adeel Ahmad
回答by Chiheb Nexus
You can do something like this using Counter
from collections
module:
您可以使用Counter
fromcollections
模块执行以下操作:
from collections import OrderedDict, Counter
import pandas as pd
a = {'key_a': 'value_a', 'key_b':'value_b'}
b = {'key_a': 'value_c', 'key_b':'value_d'}
ord_list = [OrderedDict(a), OrderedDict(b)]
col = Counter()
for k in ord_list:
col.update(k)
df = pd.DataFrame([k.values() for k in ord_list], columns = col.keys())
print(df)
Output:
输出:
key_b key_a
0 value_b value_a
1 value_d value_c
回答by Daniel Aron Goldenberg
Following @AdeelAhmad answer I needed to get the columns from one OrderDict:
按照@AdeelAhmad 的回答,我需要从一个 OrderDict 中获取列:
df = pd.DataFrame(orderedDictList, columns=orderedDictList[0].keys())
Hope it helps
希望能帮助到你
回答by Finn
df = pd.DataFrame(orderedDictList, columns=orderedDictList.keys())
This does not work because lists do not have key value pairs.
这不起作用,因为列表没有键值对。
You do not need to specify column order because orderedDicts will preserve the column order inherently. The following will work:
您不需要指定列顺序,因为orderedDicts 将固有地保留列顺序。以下将起作用:
df = pd.DataFrame(orderedDictList)