【发布时间】:2022-01-03 03:45:38
【问题描述】:
我有两个数据框。第一个数据帧有一个数组作为column2 的值,我想将它与第二个数据帧连接起来,以便将数值映射到它们的字符串值。元素的顺序应该保持不变,因为它们按索引对应于column3 中的数组元素。
df_one:
column1| column2| column3
----------------------------------
"thing1"|[1,2,3..]|[0.1,0.2,0.3..]
"thing2"|[1,2,3..]|[0.1,0.2,0.3..]
"thing3"|[1,2,3..]|[0.1,0.2,0.3..]
...
df_two:
columnA|columnB
---------------
1|"item1"
2|"item2"
3|"item3"
...
有没有办法加入这些数据框并像这样选择列:
column1 | newColumn| column3
----------------------------------------------------
"thing1"|["item1","item2","item3"..]|[0.1,0.2,0.3..]
"thing2"|["item1","item2","item3"..]|[0.1,0.2,0.3..]
"thing3"|["item1","item2","item3"..]|[0.1,0.2,0.3..]
...
【问题讨论】:
-
你可以在 'column2' 上'explode' df_one,然后加入 df_two 并在结果数据集的 'column1' 上使用 'groupBy' 和 'collect_list' 将其作为数组取回。跨度>
-
'explode' 在性能方面会是一个不错的选择吗? df_one 中至少有 100 万行,两列中的每个数组都有大约 8k 个元素。
-
'explode' 不会增加数据量,可能会影响性能的是数据混洗。看到这篇文章:stackoverflow.com/questions/52777421/…
标签: python dataframe apache-spark pyspark