我使用Spyder作为Python IDE。我已经通过API下载了数据。 API仅允许将数据下载为" dict"类型。 " Dict" type有3个级别,即Dict,Unicode,Dataframe。 我目前正在尝试将Dataframe中的信息提取到单独的DataFrame变量 中,我打算将其导出到SQLite 。但是,我无法执行此操作,因为信息保存在类型" Dict"并使用命令
frame4=pd.DataFrame.from_dict(response)
返回以下错误消息:
frame4 = pd.DataFrame(response)Traceback(最近一次调用最后一次):
文件"",第1行,in 窗口4 = pd.DataFrame(响应)
文件 " /Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/frame.py" ;, 第224行,在 init 中 mgr = self._init_dict(data,index,columns,dtype = dtype)
文件 " /Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/frame.py" ;, 第360行,在_init_dict中 return _arrays_to_mgr(arrays,data_names,index,columns,dtype = dtype)
文件 " /Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/frame.py" ;, 第5236行,在_arrays_to_mgr中 arrays = _homogenize(arrays,index,dtype)
文件 " /Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/frame.py" ;, 第5546行,在_homogenize raise_cast_failure =假)
文件 " /Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/series.py" ;, 第2922行,在_sanitize_array中 subarr = _asarray_tuplesafe(data,dtype = dtype)
文件 " /Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/common.py" ;, 第_ 1407行,在_asarray_tuplesafe中 result [:] = [值为x的元组(x)]
ValueError:无法将大小为17的序列复制到数组轴 维度10
字典的内容即dict(response)
给出以下输出:
{u'allowance': {u'allowanceExpiry': 554347,
u'remainingAllowance': 9960,
u'totalAllowance': 10000},
u'instrumentType': u'CURRENCIES',
u'prices': bid ask \
Open High Low Close Open High
DateTime
2016:08:12-21:50:00 11163.7 11164.6 11163.7 11164.1 11165.2 11165.6
2016:08:12-21:51:00 11164.2 11164.8 11163.7 11164.7 11165.7 11166.2
2016:08:12-21:52:00 11164.5 11165.3 11164.4 11165.1 11166.0 11166.6
2016:08:12-21:53:00 11165.0 11165.8 11164.3 11164.5 11166.5 11167.2
2016:08:12-21:54:00 11164.6 11165.4 11164.3 11164.7 11166.1 11166.9
2016:08:12-21:55:00 11164.6 11165.8 11164.1 11165.1 11166.1 11167.2
2016:08:12-21:56:00 11165.3 11165.3 11163.9 11163.9 11166.8 11166.8
2016:08:12-21:57:00 11164.1 11164.9 11163.4 11164.6 11165.6 11166.4
2016:08:12-21:58:00 11164.5 11165.2 11164.0 11164.9 11165.1 11166.2
2016:08:12-21:59:00 11161.3 11162.8 11157.9 11159.2 11166.3 11167.8
spread last \
Low Close Open High Low Close Open High
DateTime
2016:08:12-21:50:00 11164.7 11165.6 1.5 1.0 1.0 1.5 None None
2016:08:12-21:51:00 11165.2 11166.2 1.5 1.4 1.5 1.5 None None
2016:08:12-21:52:00 11165.8 11166.6 1.5 1.3 1.4 1.5 None None
2016:08:12-21:53:00 11165.8 11166.0 1.5 1.4 1.5 1.5 None None
2016:08:12-21:54:00 11165.8 11166.2 1.5 1.5 1.5 1.5 None None
2016:08:12-21:55:00 11165.6 11166.6 1.5 1.4 1.5 1.5 None None
2016:08:12-21:56:00 11165.4 11165.4 1.5 1.5 1.5 1.5 None None
2016:08:12-21:57:00 11164.3 11165.2 1.5 1.5 0.9 0.6 None None
2016:08:12-21:58:00 11164.6 11165.5 0.6 1.0 0.6 0.6 None None
2016:08:12-21:59:00 11162.9 11164.2 5.0 5.0 5.0 5.0 None None
Low Close Volume
DateTime
2016:08:12-21:50:00 None None 37
2016:08:12-21:51:00 None None 45
2016:08:12-21:52:00 None None 46
2016:08:12-21:53:00 None None 80
2016:08:12-21:54:00 None None 45
2016:08:12-21:55:00 None None 58
2016:08:12-21:56:00 None None 35
2016:08:12-21:57:00 None None 115
2016:08:12-21:58:00 None None 60
2016:08:12-21:59:00 None None 162 }
有人可以帮我解决这个从Dict变量中提取Dataframe到另一个Dataframe中的努力吗?
答案 0 :(得分:1)
查看您的response
字典 - 密钥prices
的值似乎是一个DataFrame,因此无需再次构建它:
frame4 = response['prices']
或简单地说:
import sqlalchemy
engine = sqlalchemy.create_engine('sqlite:///path/to/your_db.sqlite')
response['prices'].to_sql('table_name', engine, if_exists='replace')