【问题标题】:IndexError using pandas from_dict使用熊猫 from_dict 的 IndexError
【发布时间】:2015-03-25 20:37:40
【问题描述】:

当从 dict 构造数据帧时,以下示例会抛出 IndexError(index out of bounds)。

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

如果我修改dict如下,没有索引错误

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

为什么元组作为dict键会导致IndexError?

【问题讨论】:

  • 第一个 ex 在 v0.15.1 上按预期工作
  • 我使用的是 0.14.1。是时候更新了!你能发布一个答案吗?我会接受的。
  • 欢迎接受 unutbu 的一个。无论如何,她/他比我更博学。

标签: python dictionary pandas


【解决方案1】:

尝试更新您的 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

【讨论】:

    猜你喜欢
    • 2017-12-06
    • 1970-01-01
    • 2019-10-31
    • 2016-11-16
    • 1970-01-01
    • 2017-06-29
    • 2018-12-22
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多