简体   繁体   English

如何将地图转换为数据帧?

[英]How to convert map to dataframe?

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? 如何获得最终的数据帧?

First covert it to a Seq , then you can use the toDF() function. 首先将其转换为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|
+----+-------------------+

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

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