Python 从字典创建熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26074447/
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
Creating a pandas dataframe from a dictionary
提问by Jason
I'd like to create a DataFramefrom a dictwhere the dictkeyswill be the column names and the dictvalueswill be the rows. I'm trying to use pandas.DataFrame.from_dict()to convert my dictionary. Here's my code:
我想DataFrame从 a创建一个,dict其中dictkeys将是列名,而dictvalues将是行。我正在尝试使用pandas.DataFrame.from_dict()来转换我的字典。这是我的代码:
import pandas as pd
import datetime
current_time1 = datetime.datetime.now()
record_1 = {'Date':current_time1, 'Player':'John','Difficulty':'hard', 'Score':0}
df = pd.DataFrame.from_dict(record_1, orient='columns')
display(df)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-597ef27e82c8> in <module>()
1 record_1 = {'Date':current_time1, 'Player':'John','Difficulty':'hard', 'Score':0}
----> 2 df = pd.DataFrame.from_dict(record_1, orient='columns')
3 display(df)
C:\Users\Jason\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\frame.pyc in from_dict(cls, data, orient, dtype)
635 raise ValueError('only recognize index or columns for orient')
636
--> 637 return cls(data, index=index, columns=columns, dtype=dtype)
638
639 def to_dict(self, outtype='dict'):
C:\Users\Jason\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\frame.pyc in __init__(self, data, index, columns, dtype, copy)
201 dtype=dtype, copy=copy)
202 elif isinstance(data, dict):
--> 203 mgr = self._init_dict(data, index, columns, dtype=dtype)
204 elif isinstance(data, ma.MaskedArray):
205 import numpy.ma.mrecords as mrecords
C:\Users\Jason\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\frame.pyc in _init_dict(self, data, index, columns, dtype)
325
326 return _arrays_to_mgr(arrays, data_names, index, columns,
--> 327 dtype=dtype)
328
329 def _init_ndarray(self, values, index, columns, dtype=None,
C:\Users\Jason\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\frame.pyc in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
4618 # figure out the index, if necessary
4619 if index is None:
-> 4620 index = extract_index(arrays)
4621 else:
4622 index = _ensure_index(index)
C:\Users\Jason\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\frame.pyc in extract_index(data)
4657
4658 if not indexes and not raw_lengths:
-> 4659 raise ValueError('If using all scalar values, you must must pass'
4660 ' an index')
4661
ValueError: If using all scalar values, you must must pass an index
I don't understand the error, in the docsfor pandas.DataFrame.from_dictthere's no index argument. Also, I thought that if an index isn't supplied pandaswould use 1..x? How can I pass an index?
我不明白错误,在文档中pandas.DataFrame.from_dict没有索引参数。另外,我认为如果不提供索引pandas会使用 1..x?如何传递索引?
Additional information: I'd like to use the date column as the index in the end.
附加信息:我想最后使用日期列作为索引。
采纳答案by unutbu
If each dict represents a row, you could pass a list of dictsto pd.DataFrame:
如果每个 dict 代表一行,您可以将一个 dict列表传递给pd.DataFrame:
In [37]: pd.DataFrame([record_1])
Out[37]:
Date Difficulty Player Score
0 2014-09-27 08:26:16.950192 hard John 0

