簡體   English   中英

使用熊貓from_dict的IndexError

[英]IndexError using pandas from_dict

當根據字典構造數據幀時,以下示例將拋出IndexError(索引超出范圍)。

import pandas as pd
d = {(0,1):{1:'a'}}
pd.DataFrame.from_dict(d,orient='index')

如果我按以下方式修改字典,則不會出現索引錯誤

import pandas as pd
d = {1:{1:'a'}}
pd.DataFrame.from_dict(d,orient='index')

為什么將元組作為dict鍵會導致IndexError?

嘗試更新您的Pandas版本。 在版本0.15.2中, from_dict可以按預期工作:

In [18]: pd.__version__
Out[20]: '0.15.2-113-g5531341'

In [14]: d = {(0,1):{1:'a'}}

In [15]: pd.DataFrame.from_dict(d,orient='index')
Out[15]: 
     1
0 1  a

In [16]: d = {1:{1:'a'}}

In [17]: pd.DataFrame.from_dict(d,orient='index')
Out[17]: 
   1
1  a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM