【问题标题】:Can i create a dataframe from another dataframes rows我可以从另一个数据框行创建一个数据框吗
【发布时间】:2021-01-22 15:43:42
【问题描述】:

我可以使用 Pyspark 从下面的行创建一个数据框,作为新数据框的列吗?

+------------+
|         col|
+------------|
|created_meta|
|  updated_at|
|updated_meta|
|        meta|
|        Year|
|  First Name|
|      County|
|         Sex|
|       Count|
+------------

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: apache-spark pyspark apache-spark-sql


【解决方案1】:

两种方式。

  1. 使用枢轴:
df1 = df.groupBy().pivot('col').agg(F.lit(None)).limit(0)

df1.show()
+-----+------+---------+---+----+------------+----+----------+------------+
|Count|County|FirstName|Sex|Year|created_meta|meta|updated_at|updated_meta|
+-----+------+---------+---+----+------------+----+----------+------------+
+-----+------+---------+---+----+------------+----+----------+------------+
  1. 从头开始创建:
df2 = df.select([F.lit(r[0]) for r in df.collect()]).limit(0)

df2.show()
+------------+----------+------------+----+----+---------+------+---+-----+
|created_meta|updated_at|updated_meta|meta|Year|FirstName|County|Sex|Count|
+------------+----------+------------+----+----+---------+------+---+-----+
+------------+----------+------------+----+----+---------+------+---+-----+

【讨论】:

    【解决方案2】:
      // sorry in Scala + Spark
      import spark.implicits._
    
      import org.apache.spark.sql.functions._
    
      val lst  = List("created_meta",
      "updated_at",
      "updated_meta",
      "meta",
      "Year",
      "First Name",
       "County",
      "Sex",
      "Count")
    
    
      val source = lst.toDF("col")
    
      source.show(false)
      //  +------------+
      //  |col         |
      //  +------------+
      //  |created_meta|
      //  |updated_at  |
      //  |updated_meta|
      //  |meta        |
      //  |Year        |
      //  |First Name  |
      //  |County      |
      //  |Sex         |
      //  |Count       |
      //  +------------+
    
      val l = source.select('col).as[String].collect.toList
    
      val df1 = l.foldLeft(source)((acc, col) => {
        acc.withColumn(col, lit(""))
      })
    
     val df2 = df1.drop("col")
     df2.printSchema()
     //  root
     //  |-- created_meta: string (nullable = false)
     //  |-- updated_at: string (nullable = false)
     //  |-- updated_meta: string (nullable = false)
     //  |-- meta: string (nullable = false)
     //  |-- Year: string (nullable = false)
     //  |-- First Name: string (nullable = false)
     //  |-- County: string (nullable = false)
     //  |-- Sex: string (nullable = false)
     //  |-- Count: string (nullable = false)
    
    
     df2.show(1, false)
    
     //  +------------+----------+------------+----+----+----------+------+---+-----+
     //  |created_meta|updated_at|updated_meta|meta|Year|First Name|County|Sex|Count|
     //  +------------+----------+------------+----+----+----------+------+---+-----+
     //  |            |          |            |    |    |          |      |   |     |
     //  +------------+----------+------------+----+----+----------+------+---+-----+
    

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      相关资源
      最近更新 更多