scala - 在 Scala 中将数据帧转换为 Key 为 int 且 Value 为列表的 hashmap
问题描述
我有一个如下所示的数据框:
钥匙 | 字 |
---|---|
1 | ['一个测试'] |
2 | ['你好呀] |
我想创建以下哈希图:
映射(1 -> ['a', 'test'], 2 -> ['hi', 'there'])
但我无法弄清楚如何做到这一点,任何人都可以帮助我吗?
谢谢!
解决方案
一定有几十种方法可以做到这一点。一种是:
df.collect().map { case row => (row.getAs[Int](0) -> row.getAs[mutable.WrappedArray[String]](1))}.toMap
推荐阅读
- python - AirFlowException - Python_Callable 必须是可调用的
- python - 无法解决 Python ImportError: cannot import name 'FirstNews'
- javascript - Fancybox 画廊打破 swiper
- jenkins - Jenkins:Jenkins 在哪里存储分配的用户(ldap 用户)的配置文件?
- c# - 如何创建一个包含 n 个字符串属性和一个数据对象的对象列表,但对象类型及其属性可以在 C# 中动态更改
- spring - 我无法在 Spring 中通过 Thymeleaf 以 HTML 格式显示图像
- animation - Flutter TabBarView的动画持续时间
- arduino - 中断运行时Arduino不会休眠
- git - 为什么 First Commit (Root Commit) 的时间戳大于 Second Commit?
- java - 版本 8 及更高版本的 OpenJDK 安全更新