【问题标题】:KeyError: "None of [Index(['', ''], dtype='object')] are in the [columns]" when trying to select columns on a dask dataframeKeyError:“[索引(['',''],dtype ='object')]都没有在[columns]中”尝试选择dask数据帧上的列
【发布时间】:2019-10-03 21:47:12
【问题描述】:

我正在使用 from_pandas() 函数从 pandas 数据帧创建一个 dask 数据帧。当我尝试使用方括号 [[ ]] 从 dask 数据框中选择两列时,我得到一个 KeyError。

根据 dask 文档,dask 数据框支持方括号列选择,就像 pandas 数据框一样。

# data is a pandas dataframe
dask_df = ddf.from_pandas(data, 30)

data = data[dask_df[['length', 'country']].apply(
           lambda x: myfunc(x, countries),
           meta=('Boolean'),
           axis=1
       ).compute()].reset_index(drop=True)

这是我得到的错误:

KeyError: "None of [Index(['length', 'country'], dtype='object')] are in the [columns]"

我在想这可能与为应用提供正确的元数据有关,但从错误来看,dask 数据框似乎无法选择两列,这应该在应用之前发生。

如果我在应用行中将“dask_df”替换为“data”(pandas df),这将非常有效。

我在执行 from_pandas 时是否没有保留索引?

【问题讨论】:

  • 你能在 apply() 函数之前显示dask_df.columns 的输出吗?
  • 我建议生成mcve

标签: pandas dask


【解决方案1】:

尝试一次加载更少的数据。

我遇到了同样的问题,但是当我只加载了我的数据子集时,它起作用了。

使用大型数据集,我能够运行 print(dask_df.columns) 并查看例如

Index(['apple', 'orange', 'pear'], dtype='object', name='fruit').

但是当我运行dask_df.compute 时,我会得到KeyError: "None of [Index(['apple', 'orange', 'pear'], dtype='object')] are in the [columns]"

我知道数据集对我的记忆来说太大了,我正在努力希望它能帮我解决这个问题 =) 我想我还有更多工作要做,但无论如何我很高兴能加入黄昏!

【讨论】:

    【解决方案2】:

    如错误所述:列['length', 'country']

    dask_df 中不存在。 先创建它们,然后再运行你的函数。

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2021-10-29
      • 2021-01-08
      • 1970-01-01
      • 2017-01-07
      • 2020-05-10
      • 2022-11-18
      • 2023-01-30
      • 2021-11-19
      相关资源
      最近更新 更多