首页 > 解决方案 > AWS Glue 将列值添加为另一个 DynamicFrame 中的列

问题描述

我是 AWS Glue 和 Pyspark 的新手,所以我在转换工作时遇到了一些麻烦。我有两个 DynamicFrame,其中一个包含其中一个列中的值,需要将其作为单独的列添加到另一个 DF 中,并且该列中的值需要是与另一列中具有相同值的值相对应的值id 在第一个表中。这是它的外观:

Table 1             Table2
+--+-----+-----+    +--+-----+-----+
|id|name |value|    |id|col1 |col2 |
+--+-----+-----+    +--+-----+-----+
| 1|name1| 10  |    | 1|str1 |val1 |
+--+-----+-----+    +--+-----+-----+
| 2|name2| 20  |    | 2|str2 |val2 |
+--+-----+-----+    +--+-----+-----+

我需要新格式为:

Table2
+--+-----+-----+-----+-----+
|id|col1 |col2 |name1|name2|
+--+-----+-----+-----+-----+
| 1|str1 |val1 | 10  |     |  <--- add 10 only here because the id from the row in the first table must match the id from the second table
+--+-----+-----+-----+-----+
| 2|str2 |val2 |     | 20  |  <--- add 20 only here because the id from the row in the first table must match the id from the second table
+--+-----+-----+-----+-----+

标签: pythonpysparkaws-glue

解决方案


假设 2 个数据帧被命名为df1df2

df3 = df1.groupBy('id').pivot('name').sum('value')
df4 = df2.join(df3, on='id', how='inner')
df4.show(truncate=False)

推荐阅读