代码之家  ›  专栏  ›  技术社区  ›  Shekhar

如何从Spark数据框中删除多列?

  •  1
  • Shekhar  · 技术社区  · 7 年前

    我有一个CSV,其中一些列标题及其对应的值为null。我想知道如何删除具有名称的列 null ? CSV示例如下:

    "name"|"age"|"city"|"null"|"null"|"null"
    "abcd"|"21" |"7yhj"|"null"|"null"|"null"
    "qazx"|"31" |"iuhy"|"null"|"null"|"null"
    "foob"|"51" |"barx"|"null"|"null"|"null"
    

    我想删除所有包含标题的列 无效的 这样输出数据帧将如下所示:

    "name"|"age"|"city"
    "abcd"|"21" |"7yhj"
    "qazx"|"31" |"iuhy"
    "foob"|"51" |"barx"
    

    当我在spark中加载这个CSV时,spark会将数字附加到空列,如下所示:

    "name"|"age"|"city"|"null4"|"null5"|"null6"
    "abcd"|"21" |"7yhj"|"null"|"null"|"null"
    "qazx"|"31" |"iuhy"|"null"|"null"|"null"
    "foob"|"51" |"barx"|"null"|"null"|"null"
    

    谢谢@MaxU的回答。我的最终解决方案是:

    val filePath = "C:\\Users\\shekhar\\spark-trials\\null_column_header_test.csv"
    
    val df = spark.read.format("csv")
    .option("inferSchema", "false")
    .option("header", "true")
    .option("delimiter", "|")
    .load(filePath)
    
    val q = df.columns.filterNot(c => c.startsWith("null")).map(a => df(a))
    // df.columns.filterNot(c => c.startsWith("null")) this part removes column names which start with null and returns array of string. each element of array represents column name
    
    // .map(a => df(a)) converts elements of array into object of type Column
    df.select(q:_*).show
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   MaxU - stand with Ukraine    7 年前

    IIUC你可以这样做:

    df = df.drop(df.columns.filter(_.startsWith("null")))