python - 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
+--+-----+-----+-----+-----+
解决方案
假设 2 个数据帧被命名为df1
和df2
。
df3 = df1.groupBy('id').pivot('name').sum('value')
df4 = df2.join(df3, on='id', how='inner')
df4.show(truncate=False)
推荐阅读
- r - 绘制字符
- node.js - 希望在 nestjs 解析器中使用 delegateToSchema
- android - Android:如何配置具有背景颜色和居中图像的加载屏幕
- python - Postgresql 和 Flask 之间的 REST 请求
- azure-bot-service - Azure QnA Maker 更新置信度
- python - 管道的 Azure ML 输出
- c# - 错误 CS0229 'CreateModel._context' 和 'CreateModel._context' 之间的歧义
- gnuplot - Gnuplot:在一个图表中组合聚集直方图和行堆叠直方图样式
- python - ImportError:无法在 AWS Sagemaker 中导入名称 np_utils
- linux - Cordova build ios`无法将'../acorn/bin/acorn'复制到自身的子目录'../acorn/bin/acorn'。