【问题标题】:RDD skip headers - PysparkRDD 跳过标头 - Pyspark
【发布时间】:2018-04-12 09:28:23
【问题描述】:

我想读取带有标头的 RDD。我在这里发现了类似的问题,但它对我不起作用。 How do I skip a header from CSV files in Spark?

rdd.mapPartitionsWithIndex { (idx, iter) => if (idx == 0) iter.drop(1)

否则迭代}

所以我尝试了

def f(idx, iter): 
    if idx==0:
        iter.drop(1)
    else:
        yield list(iterator)
rdd2 = rdd.mapPartitionsWithIndex(f)

但它说 AttributeError: 'generator' object has no attribute 'drop'

有什么帮助吗?

【问题讨论】:

  • 通过收集标题并过滤掉找到了简单的方法,但我想了解更多关于 mapPartitions 的工作原理。

标签: pyspark rdd


【解决方案1】:

试试这样的:

def f(idx, iter):
    output=[]
    for sublist in iter:
        output.append(sublist)
    if idx>0:
        return(output)
    else:
        return(output[1:])

【讨论】:

  • 它有效!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多