【问题标题】:SparkPandasNotImplementedError: .iloc requires numeric slice or conditional boolean IndexSparkPandasNotImplementedError:.iloc 需要数字切片或条件布尔索引
【发布时间】:2020-01-21 04:40:36
【问题描述】:

我在 Databricks 上不断收到以下错误:

SparkPandasNotImplementedError: .iloc requires numeric slice or conditional boolean Index, got You are trying to use pandas function .iloc[..., ...], use spark function select, where

这是我的代码:

import re 
import nltk
import heapq
corpus = []
for i in range(0, len(Y)):
    describe = re.sub('[^a-zA-Z]', ' ', Y.iloc[i, 0])
    describe = describe.lower()
    describe = describe.split()
    describe = ' '.join(describe)
    corpus.append(describe)

代码在 Spyder 中运行良好,但在数据块中却不行。

【问题讨论】:

    标签: python pandas error-handling apache-spark-sql databricks


    【解决方案1】:

    我尝试成功重现与您相同的问题,如下代码和图所示。

    import numpy as np
    import pandas as pd
    import databricks.koalas as ks
    dates = pd.date_range('20130101', periods=6)
    pdf = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    df = ks.from_pandas(pdf)
    print(pdf.iloc[0,0])
    print(df.iloc[0,0])
    

    由于缺少对变量Y 的必要描述,我猜Y 是一个数据框,但区别是本地Spyder 上的pandas 数据框,数据块中的Koalas 数据框。

    根据databricks.koalas.DataFrame.iloc的Koalas文档,它不支持对Koalas数据框的操作iloc(int, int)

    所以如果你想对databricks中每一行的第一列值做一些操作,有以下两种解决方案。

    1. 确保 Y 是您数据块的同一脚本中的 pandas 数据框。
    2. Y必须是你想要的考拉数据框,请尝试如下代码。

      # Here, `Y` is a Koalas dataframe
      for row in Y.iterrows():
          describe = re.sub('[^a-zA-Z]', ' ', row[1][0])
          describe = describe.lower()
          describe = describe.split()
          describe = ' '.join(describe)
          corpus.append(describe)
      

      正如您在下面看到我的示例代码和结果,函数iterrows 可以帮助获取每行的第一列值。

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 2017-04-04
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多