【发布时间】:2020-07-07 12:57:38
【问题描述】:
我创建了一个函数来测试 DataFrame 上的转换。这仅返回转换后的列。
def test_concat(df: sd.DataFrame, col_names: list) -> sd.DataFrame:
return df.select(*[F.concat(df[column].cast(StringType()), F.lit(" new!")).alias(column) for column in col_names])
如何将现有列替换为原始 DF 中的转换一次并返回整个 DF?
示例 DF:
test_df = self.spark.createDataFrame([(1, 'metric1', 10), (2, 'metric2', 20), (3, 'metric3', 30)], ['id', 'metric', 'score'])
cols = ["metric"]
new_df = perform_concat(test_df, cols)
new_df.show()
预期结果:
|metric | score |
+-------------+--------+
|metric1 new! | 10 |
|metric2 new! | 20 |
|metric3 new! | 30 |
看起来我可以从 DF 中删除原始列,然后以某种方式附加转换后的列。但不确定这是实现这一目标的正确方法。
【问题讨论】:
-
能否分享一些 Impute 和预期输出的示例数据
-
@dsk 我已经更新了我的问题
-
请检查一下
-
为更清晰添加了一些屏幕截图,请检查它们。
标签: python python-3.x dataframe pyspark