代码之家  ›  专栏  ›  技术社区  ›  Roman Kagan mianos

如何只显示Spark数据框架中的相关列?

  •  0
  • Roman Kagan mianos  · 技术社区  · 7 年前

    我有一个大的JSON文件,有432个键值对和许多这样的数据行。这个数据加载得很好,但是当我想使用df.show()显示20个项目时,我看到一堆空值。文件非常稀疏。很难从中得到什么。最好的方法是删除20行中只有空值的列,但是考虑到我有很多键值对,很难手工操作。有没有一种方法可以在Spark的数据框架中检测哪些列只包含空值并将其丢弃?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Sathiyan S    7 年前

    你可以尝试如下,了解更多信息, referred_question

    scala> val df = Seq((1,2,null),(3,4,null),(5,6,null),(7,8,"9")).toDF("a","b","c")
    
    scala> df.show
    +---+---+----+
    |  a|  b|   c|
    +---+---+----+
    |  1|  2|null|
    |  3|  4|null|
    |  5|  6|null|
    |  7|  8|   9|
    +---+---+----+
    
    scala> val dfl = df.limit(3) //limiting the number of rows you need, in your case it is 20
    
    scala> val col_names = dfl.select(dfl.columns.map(x => count(col(x)).alias(x)):_*).first.toSeq.zipWithIndex.filter(x => x._1.toString.toInt > 0).map(_._2).map(x => dfl.columns(x)).map(x => col(x)) // this will give you column names which is having not null values
    col_names: Seq[org.apache.spark.sql.Column] = ArrayBuffer(a, b)
    
    scala> dfl.select(col_names : _*).show
    +---+---+
    |  a|  b|
    +---+---+
    |  1|  2|
    |  3|  4|
    |  5|  6|
    +---+---+
    

    让我知道它是否适合你。

        2
  •  1
  •   stack0114106    7 年前

    类似于Sathiyan的想法,但是在count()本身中使用columnname。

    scala>  val df = Seq((1,2,null),(3,4,null),(5,6,null)).toDF("a","b","c")
    df: org.apache.spark.sql.DataFrame = [a: int, b: int ... 1 more field]
    
    scala> df.show
    +---+---+----+
    |  a|  b|   c|
    +---+---+----+
    |  1|  2|null|
    |  3|  4|null|
    |  5|  6|null|
    +---+---+----+
    
    
    scala> val notnull_cols = df.select(df.columns.map(x=>concat_ws("=",first(lit(x)),count(col(x)))):_*).first.toSeq.map(_.toString).filter(!_.contains("=0")).map( x=>col(x.split("=")(0)) )
    notnull_cols: Seq[org.apache.spark.sql.Column] = ArrayBuffer(a, b)
    
    scala> df.select(notnull_cols:_*).show
    +---+---+
    |  a|  b|
    +---+---+
    |  1|  2|
    |  3|  4|
    |  5|  6|
    +---+---+
    

    中间结果显示计数和列名

    scala> df.select(df.columns.map(x=>concat_ws("=",first(lit(x)),count(col(x))).as(x+"_nullcount")):_*).show
    +-----------+-----------+-----------+
    |a_nullcount|b_nullcount|c_nullcount|
    +-----------+-----------+-----------+
    |        a=3|        b=3|        c=0|
    +-----------+---------- -+-----------+
    
    
    scala>