繁体   English   中英

使用 scala 在 Spark 中将 DataFrame 单行转换为列

[英]Transpose DataFrame single row to column in Spark with scala

我在这里看到了这个问题: Transpose DataFrame without Aggregation in Spark with scala而我想做的恰恰相反。

我有一个单行的 Dataframe,其值为字符串、整数、布尔值、数组:

+-----+-------+-----+------+-----+
|col1 | col2  |col3 | col4 |col5 |
+-----+-------+-----+------+-----+
|val1 | val2  |val3 | val4 |val5 |
+-----+-------+-----+------+-----+

我想像这样转置它:

+-----------+-------+
|Columns    | values|
+-----------+-------+
|col1       | val1  |
|col2       | val2  |
|col3       | val3  |
|col4       | val4  |
|col5       | val5  |
+-----------+-------+

我正在使用 Apache Spark 2.4.3 和 Scala 2.11

编辑:值可以是任何类型(int、double、bool、array),而不仅仅是字符串。

Spark-2.4使用arrays_ziparray(column_values), array(column_names)然后分解得到结果。

Example:

val df=Seq((("val1"),("val2"),("val3"),("val4"),("val5"))).toDF("col1","col2","col3","col4","col5")

val cols=df.columns.map(x => col(s"${x}"))

val str_cols=df.columns.mkString(",")

df.withColumn("new",explode(arrays_zip(array(cols:_*),split(lit(str_cols),",")))).
select("new.*").
toDF("values","Columns").
show()
//+------+-------+
//|values|Columns|
//+------+-------+
//|  val1|   col1|
//|  val2|   col2|
//|  val3|   col3|
//|  val4|   col4|
//|  val5|   col5|
//+------+-------+

UPDATE:

val df=Seq(((2),(3),(true),(2.4),("val"))).toDF("col1","col2","col3","col4","col5")

df.printSchema
//root
// |-- col1: integer (nullable = false)
// |-- col2: integer (nullable = false)
// |-- col3: boolean (nullable = false)
// |-- col4: double (nullable = false)
// |-- col5: string (nullable = true)

//cast to string
val cols=df.columns.map(x => col(s"${x}").cast("string").alias(s"${x}"))

val str_cols=df.columns.mkString(",")

df.withColumn("new",explode(arrays_zip(array(cols:_*),split(lit(str_cols),",")))).
select("new.*").
toDF("values","Columns").
show()

//+------+-------+
//|values|Columns|
//+------+-------+
//|     2|   col1|
//|     3|   col2|
//|  true|   col3|
//|   2.4|   col4|
//|   val|   col5|
//+------+-------+

不使用arrays_zip (在=> Spark 2.4中可用)的想法有所不同]并得到以下...

它将以更简单的方式map flatmap explode功能)...

此处map function(用于列)创建一个新的 map 列。 输入列必须分组为键值对。

案例:数据中的字符串数据类型:

import org.apache.spark.sql.functions._

val df: DataFrame =Seq((("val1"),("val2"),("val3"),("val4"),("val5"))).toDF("col1","col2","col3","col4","col5")

var columnsAndValues = df.columns.flatMap { c => Array(lit(c), col(c)) }
df.printSchema()

df.withColumn("myMap", map(columnsAndValues:_*)).select(explode($"myMap"))
  .toDF("Columns","Values").show(false)

结果:

root
 |-- col1: string (nullable = true)
 |-- col2: string (nullable = true)
 |-- col3: string (nullable = true)
 |-- col4: string (nullable = true)
 |-- col5: string (nullable = true)

+-------+------+
|Columns|Values|
+-------+------+
|col1   |val1  |
|col2   |val2  |
|col3   |val3  |
|col4   |val4  |
|col5   |val5  |
+-------+------+

案例:数据中数据类型的混合:

如果您有不同的类型将它们转换为字符串...其余步骤不会改变..

val df1 = df.select(df.columns.map(c => col(c).cast(StringType)): _*)

完整示例:

import org.apache.spark.sql.functions._
import spark.implicits._
import org.apache.spark.sql.Column

val df = Seq(((2), (3), (true), (2.4), ("val"))).toDF("col1", "col2", "col3", "col4", "col5")
df.printSchema()
/**
  * convert all columns to  to string type since its needed further
  */
val df1 = df.select(df.columns.map(c => col(c).cast(StringType)): _*)
df1.printSchema()
var ColumnsAndValues: Array[Column] = df.columns.flatMap { c => {
    Array(lit(c), col(c))
  }
}

df1.withColumn("myMap", map(ColumnsAndValues: _*))
   .select(explode($"myMap"))
   .toDF("Columns", "Values")
   .show(false)

结果:

root
 |-- col1: integer (nullable = false)
 |-- col2: integer (nullable = false)
 |-- col3: boolean (nullable = false)
 |-- col4: double (nullable = false)
 |-- col5: string (nullable = true)

root
 |-- col1: string (nullable = false)
 |-- col2: string (nullable = false)
 |-- col3: string (nullable = false)
 |-- col4: string (nullable = false)
 |-- col5: string (nullable = true)

+-------+------+
|Columns|Values|
+-------+------+
|col1   |2     |
|col2   |3     |
|col3   |true  |
|col4   |2.4   |
|col5   |val   |
+-------+------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM