Python 从dict生成数据帧
我有这样的数据:Python 从dict生成数据帧,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有这样的数据: >>> cc defaultdict(<class 'dict'>, {1540272960: {'max': 1.14614, 'to': 1540273020, 'close': 1.14606, 'from': 1540272960, 'open': 1.145935, 'volume': 96, 'id': 366597, 'min': 1.14593, 'at': 1540273020040554921}, 1540273020: {'a
>>> cc
defaultdict(<class 'dict'>, {1540272960: {'max': 1.14614, 'to': 1540273020, 'close': 1.14606, 'from': 1540272960, 'open': 1.145935, 'volume': 96, 'id': 366597, 'min': 1.14593, 'at': 1540273020040554921}, 1540273020: {'active_id': 1, 'to': 1540273080, 'ask': 1.14622, 'open': 1.14606, 'max_at': 1540273034, 'size': 60, 'max': 1.146135, 'at': 1540273040013821491, 'min_at': 1540273020, 'close': 1.146095, 'from': 1540273020, 'volume': 42, 'bid': 1.14597, 'id': 366598, 'min': 1.14606}})
我明白了:
>>> df.index
Index(['active_id', 'ask', 'at', 'bid', 'close', 'from', 'id', 'max', 'max_at',
'min', 'min_at', 'open', 'size', 'to', 'volume'],
dtype='object')
及
df[“卷”]
回溯(最近一次呼叫最后一次):
pandas.\u libs.index.IndexEngine.get\u loc中第162行的文件“pandas\\u libs\index.pyx”
pandas.\u libs.hashtable.Int64HashTable.get\u项目中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第958行
TypeError:需要一个整数
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Python35\lib\site packages\pandas\core\index\base.py”,第3078行,在get\u loc中
返回发动机。获取位置(钥匙)
pandas.\u libs.index.IndexEngine.get\u loc文件“pandas\\u libs\index.pyx”,第140行
pandas.\u libs.index.IndexEngine.get\u loc中第164行的文件“pandas\\u libs\index.pyx”
KeyError:“卷”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
pandas.\u libs.index.IndexEngine.get\u loc中第162行的文件“pandas\\u libs\index.pyx”
pandas.\u libs.hashtable.Int64HashTable.get\u项目中的文件“pandas\\u libs\hashtable\u class\u helper.pxi”,第958行
TypeError:需要一个整数
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python35\lib\site packages\pandas\core\frame.py”,第2688行,在\uu getitem中__
返回self.\u getitem\u列(键)
文件“C:\Python35\lib\site packages\pandas\core\frame.py”,第2695行,在\u getitem\u列中
返回self.\u获取\u项目\u缓存(密钥)
文件“C:\Python35\lib\site packages\pandas\core\generic.py”,第2489行,在\u get\u item\u缓存中
values=self.\u data.get(项目)
get中第4115行的文件“C:\Python35\lib\site packages\pandas\core\internals.py”
loc=自身项目。获取loc(项目)
文件“C:\Python35\lib\site packages\pandas\core\index\base.py”,第3080行,在get\u loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
pandas.\u libs.index.IndexEngine.get\u loc文件“pandas\\u libs\index.pyx”,第140行
pandas.\u libs.index.IndexEngine.get\u loc中第164行的文件“pandas\\u libs\index.pyx”
KeyError:“卷”
但这些值是以垂直数据框的形式出现的。我希望索引中的键和值必须相应地放在相应的列中。我如何才能做到这一点?使用:
另一个想法是使用转置:
或者与@jezrael的第二个类似,但使用了
transopse
:
df = pd.DataFrame(cc).transpose()
然后:
是:
正如预期的那样您的意思是轴被翻转了?使用转置。df=df.T或在创建帧时添加.T。哇,好主意。。。。这也有帮助。嘿,笑脸真是太棒了。。。它是怎么出现的。。。我试着用:)。。但是它不起作用。。你的评论看起来很好,很有活力。我将试着复制你的一条。。
>>> df["volume"]
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: 'volume'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "C:\Python35\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "C:\Python35\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "C:\Python35\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:\Python35\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: 'volume'
df = pd.DataFrame.from_dict(cc, orient='index')
print (df)
max to close from open volume \
1540272960 1.146140 1540273020 1.146060 1540272960 1.145935 96
1540273020 1.146135 1540273080 1.146095 1540273020 1.146060 42
id min at active_id ask \
1540272960 366597 1.14593 1540273020040554921 NaN NaN
1540273020 366598 1.14606 1540273040013821491 1.0 1.14622
max_at size min_at bid
1540272960 NaN NaN NaN NaN
1540273020 1.540273e+09 60.0 1.540273e+09 1.14597
df = pd.DataFrame(cc).T
df = pd.DataFrame(cc).transpose()
print(df)
max to close from open volume \
1540272960 1.146140 1540273020 1.146060 1540272960 1.145935 96
1540273020 1.146135 1540273080 1.146095 1540273020 1.146060 42
id min at active_id ask \
1540272960 366597 1.14593 1540273020040554921 NaN NaN
1540273020 366598 1.14606 1540273040013821491 1.0 1.14622
max_at size min_at bid
1540272960 NaN NaN NaN NaN
1540273020 1.540273e+09 60.0 1.540273e+09 1.14597