简体   繁体   中英

IndexError using pandas from_dict

When constructing dataframe from a dict, the following example, throws an IndexError(index out of bounds).

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

If I modify the dict as below, there is no index error

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

Why does a tuple as the dict key cause the IndexError?

Try updating your version of Pandas. In version 0.15.2, from_dict works as expected:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM