从字典以 {index: list of row values} 的形式构造 Pandas DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27665988/
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
Construct Pandas DataFrame from dictionary in form {index: list of row values}
提问by birone
I've managed to do this using:
我已经设法做到这一点:
dft = pd.DataFrame.from_dict({
0: [50, 45, 00, 00],
1: [53, 48, 00, 00],
2: [56, 53, 00, 00],
3: [54, 49, 00, 00],
4: [53, 48, 00, 00],
5: [50, 45, 00, 00]
}, orient='index'
)
Done like this, the constructor looks just like the DataFrame making it easy to read/edit:
这样做后,构造函数看起来就像 DataFrame 一样,易于阅读/编辑:
>>> dft
0 1 2 3
0 50 45 0 0
1 53 48 0 0
2 56 53 0 0
3 54 49 0 0
4 53 48 0 0
5 50 45 0 0
But the DataFrame.from_dict constructordoesn't have a columns parameter, so giving the columns sensible names takes an additional step:
但是DataFrame.from_dict 构造函数没有 columns 参数,因此为列提供合理的名称需要额外的步骤:
dft.columns = ['A', 'B', 'C', 'D']
This seems clunky for such a handy (e.g. for unit tests) way to initialise DataFrames.
这对于初始化 DataFrame 的这种方便(例如用于单元测试)的方式来说似乎很笨拙。
So I wonder: is there a better way?
所以我想知道:有没有更好的方法?
采纳答案by Alex Riley
Alternatively you could use DataFrame.from_items()to construct the DataFrame from your dictionary; this allows you to pass in the column names at the same time.
或者,您可以使用DataFrame.from_items()从字典中构造 DataFrame;这允许您同时传入列名。
For example, if dis your dictionary:
例如,如果d是您的字典:
d = {0: [50, 45, 0, 0],
1: [53, 48, 0, 0],
2: [56, 53, 0, 0],
3: [54, 49, 0, 0],
4: [53, 48, 0, 0],
5: [50, 45, 0, 0]}
The data is d.items()and the orient is again 'index'. The dictionary keys become the index values:
数据是d.items(),东方又是'index'。字典键成为索引值:
>>> pd.DataFrame.from_items(d.items(),
orient='index',
columns=['A','B','C','D'])
A B C D
0 50 45 0 0
1 53 48 0 0
2 56 53 0 0
3 54 49 0 0
4 53 48 0 0
5 50 45 0 0
In Python 2 you can use d.iteritems()to yield the contents of the dictionary to avoid creating another list in memory.
在 Python 2 中,您可以使用d.iteritems()生成字典的内容以避免在内存中创建另一个列表。
回答by grasshopper
One way to do that is the following:
一种方法是:
df = pd.DataFrame.from_dict({
0: {"A":50, "B":40},
1: {"A":51, "B":30}}, orient='index')
However, for quick test initialization I would probably prefer your way + then setting the columns.
但是,为了快速测试初始化,我可能更喜欢你的方式 + 然后设置列。
回答by tschm
You could try:
你可以试试:
x=pd.DataFrame({0:[50,45],1:[53,48],2:[56,53]}, index=["A","B"]).transpose()
But it's still odd as you are specifying the standard index as keys for your dictionary.
但这仍然很奇怪,因为您将标准索引指定为字典的键。
Why not directly
为什么不直接
x = pd.DataFrame({"A":[50,53,56],"B":...})

