首页 文章

Python Pandas dict到dataframe(不工作)

提问于
浏览
2

我使用Spyder作为Python IDE . 我已经通过API下载了数据 . API仅允许将数据下载为"dict"类型 . "Dict"类型有3个级别,即Dict,Unicode,Dataframe . 我目前正在尝试将Dataframe中的信息提取到单独的DataFrame变量 which I intend to export to SQLite 中 . 但是,我无法执行此操作,因为信息保存在"Dict"类型和使用命令下

frame4=pd.DataFrame.from_dict(response)

返回以下错误消息:

frame4 = pd.DataFrame(response)Traceback(最近一次调用最后一次):文件“”,第1行,第4行,第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中返回_arrays_to_mgr(数组,数据名,索引,列,dtype = dtype)文件”/Users/Sebster/anaconda/lib/python2.7/site-packages/pandas /core/frame.py“,第5236行,在_arrays_to_mgr数组= _homogenize(数组,索引,dtype)文件”/Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/frame.py“ ,第5546行,_homogenize raise_cast_failure = False)文件“/Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/series.py”,第2922行,在_sanitize_array中subarr = _asarray_tuplesafe(data,dtype) = dtype)在_asarray_t中输入文件“/Users/Sebster/anaconda/lib/python2.7/site-packages/pandas/core/common.py”,第1407行uplesafe result [:] = [值为x的元组的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中的努力吗?

1 回答

  • 1

    查看你的 response dict - 键 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')
    

相关问题