【问题标题】:Join two pyspark dataframes to select all the columns from the first df and some columns from the second df加入两个 pyspark 数据框以选择第一个 df 中的所有列和第二个 df 中的一些列
【发布时间】:2020-01-24 14:38:05
【问题描述】:

我尝试导入如下所示的两个函数,但出现错误

from pyspark.sql.functions import regexp_replace, col

df1 = sales.alias('a').join(customer.alias('b'),col('b.ID') == col('a.ID'))\
           .select([col('a.'+xx) for xx in sales.columns] + col('b.others')

TypeError: 'str' object is not callable

我真的不明白那行代码有什么问题?谢谢。

【问题讨论】:

  • 你能不能过去所有的 df1 表达式,因为它不完整

标签: python-3.x apache-spark pyspark pyspark-sql


【解决方案1】:

PySpark 选择函数只需要字符串列名,不需要将列对象作为数组发送。所以你可能只需要这样做

from pyspark.sql.functions import regexp_replace, col

df1 = sales.alias('a').join(customer.alias('b'),col('b.ID') == col('a.ID'))\
           .select(sales.columns + ['others'])

【讨论】:

  • 由于某些原因,它不起作用。我改变了我的代码如下,它可以工作df1 = sales.join(customer, on=['ID']).select(sales.columns + ['others'])当我添加alias时出现一些问题@
猜你喜欢
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多