python - 在多列上使用 df.withColumn()

标签 python python-2.7 pyspark apache-spark-sql spss-modeler

我正在使用 python 和 pyspark 来扩展 SPSS Modeler。

我想操作 ~5000 列,因此使用以下构造:

for target in targets:
    inputData = inputData.withColumn(target+appendString, function(target))

这非常慢。有没有更有效的方法来对所有目标列执行此操作?

targets 包含要使用的列名称列表,function(target) 是一个占位符,我可以在其中对不同的列进行操作,例如添加和除法。

如果你能帮助我,我会很高兴:)

潘达约

最佳答案

试试这个:

inputData.select(
    '*', 
    *(function(target).alias(target+appendString) for target in targets)
)

关于python - 在多列上使用 df.withColumn(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49981833/

相关文章:

python - 动态单选按钮创建

algorithm - 监督机器学习,产生训练有素的估计器

hadoop - 将大量 Spark 数据帧合并为一个

python - 如何在不聚合原始 RDD 分区的情况下对多个 RDD 进行分组?

python - 优化Pyspark的Collect_List函数

python - 为什么相等整数的行为与相等列表不同?

python - 系列与 Pandas 中 DataFrame 的每一列的关联,矢量化

python - 如何完全反汇编 Python 源代码

python - 如何打印reStructuredText节点树?

python - 使用python从网页中提取某些文本行