首页 > 解决方案 > Spark,如何从数据框中获取旋转的列名?

问题描述

我旋转一列,它会生成多个新列。

我想获取这些列并将其打包在一个字段下。

下面的代码给了我想要的结果。
但是我手动选择col("search"), col("main"), col("theme"),我想知道是否有一种方法可以动态选择所有这些列(我可以说是透视列吗?))

 # I'm going to pivot on the 2nd column
 mylist = [
     [1, 'search', 3, 1],
     [1, 'search', 3, 2],
     [1, 'main', 5, 3],
     [1, 'main', 6, 4],

     [2, 'search', 4, 10],
     [2, 'search', 4, 11],
     [2, 'main', 6, 12],
     [2, 'main', 6, 13],
     [2, 'theme', 6, 14],

     [3, 'search', 4, 5],
     [3, 'main', 6, 6],
     [3, 'main', 6, 7],
     [3, 'theme', 6, 8],
 ]

 df = pd.DataFrame(mylist, columns=['id', 'origin', 'time', 'screen_index'])

 mylist = df.to_dict('records')
 spark_session = get_spark_session()

 df = spark_session.createDataFrame(Row(**x) for x in mylist)

 df_wanted = df.groupBy("id").pivot('origin').agg(
     struct(count(lit(1)).alias('count'), avg("time").alias('avg_time'))
 ).withColumn(
     #### here I'm manually selecting columns, but want to grab them dynamically because I don't know beforehand what they gonna be.
     "origin_info", struct(col("search"), col("main"), col("theme")) 
 ).select("id", "origin_info")


 df_wanted.printSchema()
 root
  |-- id: long (nullable = true)
  |-- origin_info: struct (nullable = false)
  |    |-- search: struct (nullable = false)
  |    |    |-- count: long (nullable = false)
  |    |    |-- avg_time: double (nullable = true)
  |    |-- main: struct (nullable = false)
  |    |    |-- count: long (nullable = false)
  |    |    |-- avg_time: double (nullable = true)
  |    |-- theme: struct (nullable = false)
  |    |    |-- count: long (nullable = false)
  |    |    |-- avg_time: double (nullable = true)

标签: apache-sparkpysparkapache-spark-sql

解决方案


其实我想我想通了。
虽然我不知道它的性能..

我从https://stackoverflow.com/a/41011195/433570得到了提示

names = df_wanted.schema.names.copy()
names.remove("id")

columns = [col(name) for name in names]


df_wanted = df_wanted.withColumn(
    "origin_info", struct(*columns)
).select("id", "origin_info")

推荐阅读