首页 > 解决方案 > 在 Scala 中将数据帧转换为 Key 为 int 且 Value 为列表的 hashmap

问题描述

我有一个如下所示的数据框:

钥匙
1 ['一个测试']
2 ['你好呀]

我想创建以下哈希图:

映射(1 -> ['a', 'test'], 2 -> ['hi', 'there'])

但我无法弄清楚如何做到这一点,任何人都可以帮助我吗?

谢谢!

标签: scaladataframehashmap

解决方案


一定有几十种方法可以做到这一点。一种是:

df.collect().map { case row => (row.getAs[Int](0) -> row.getAs[mutable.WrappedArray[String]](1))}.toMap

推荐阅读