pyspark 条件_如何在pyspark中使用多条件连接?

本文讨论了在Pyspark中使用DataFrame的join操作时遇到的问题,特别是当尝试结合单个列条件和表达式条件时。通过一个具体的例子展示了如何创建DataFrame、定义条件并尝试进行左外连接。错误信息表明'Column'对象不可调用。解决方案包括正确地构造join条件,确保同时考虑列名和表达式。
摘要由CSDN通过智能技术生成

我可以将dataframe join语句与single-on条件一起使用(在pyspark中),但是,如果我尝试添加多个条件,则它将失败。

代码:summary2 = summary.join(county_prop, ["category_id", "bucket"], how = "leftouter").

以上代码有效。但是,如果我为list添加一些其他条件,比如summary.bucket==9或其他内容,那么它将失败。请帮我解决这个问题。The error for the statement

summary2 = summary.join(county_prop, ["category_id", (summary.bucket)==9], how = "leftouter")

ERROR : TypeError: 'Column' object is not callable

编辑:

添加完整的工作示例。schema = StructType([StructField("category", StringType()), StructField("category_id", StringType()), StructField("bucket", StringType()), StructField("prop_count", StringType()), StructField("event_count", StringType()), StructField("accum_prop_count",StringType())])

bucket_summary = sqlContext.createDataFrame([],schema)

temp_county_prop = sqlContext.createDataFrame([("nation","nation",1,222,444,555),("nation","state",2,222,444,555)],schema)

bucket_summary = bucket_summary.unionAll(temp_county_prop)

county_prop = sqlContext.createDataFrame([("nation","state",2,121,221,551)],schema)

要在上进行联接:

category_id和bucket列,我想替换country_prop on bucket_summary的值。cond = [bucket_summary.bucket == county_prop.bucket, bucket_summary.bucket == 2]

bucket_summary2=bucket_summary.join(country_prop,cond,how=“leftouter”)1. It works if I mention the whole statement with cols, but if I list conditions like ["category_id", "bucket"] --- THis too works.

2. But, if I use a combination of both like cond =["bucket", bucket_summary.category_id == "state"]

它不起作用。这两种说法有什么问题?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值