scala 如何将地图转换为数据框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46317098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:27:02  来源:igfitidea点击:

How to convert map to dataframe?

scalaapache-sparkdictionaryapache-spark-sql

提问by gary yong

m is a map as following:

m 是一个映射如下:

scala> m
res119: scala.collection.mutable.Map[Any,Any] = Map(A-> 0.11164610291904906, B-> 0.11856755943424617, C -> 0.1023171832681312)

I want to get:

我想得到:

name  score
A  0.11164610291904906
B  0.11856755943424617
C  0.1023171832681312

How to get the final dataframe?

如何获得最终的数据帧?

回答by Shaido - Reinstate Monica

First covert it to a Seq, then you can use the toDF()function.

首先将其转换为 a Seq,然后您就可以使用该toDF()功能。

val spark = SparkSession.builder.getOrCreate()
import spark.implicits._

val m = Map("A"-> 0.11164610291904906, "B"-> 0.11856755943424617, "C" -> 0.1023171832681312)
val df = m.toSeq.toDF("name", "score")
df.show

Will give you:

会给你:

+----+-------------------+
|name|              score|
+----+-------------------+
|   A|0.11164610291904906|
|   B|0.11856755943424617|
|   C| 0.1023171832681312|
+----+-------------------+