pyspark加入乘以普通列

发布于 2025-02-07 18:26:42 字数 657 浏览 5 评论 0原文

美好的一天,

我正在加入Pyspark上的数据。来自SQL,我喜欢通过这样的通用密钥定义加入。

data_want = data_1.join(data_2, data_1.common_key == data_2.common_key , 'left' )
data_want.columns 

[< normal_columns>,common_key,common_key]

我得到 common_key -column的双重条目。非常奇怪。使用较短的语法执行此操作时:

data_want = data_1.join(data_2, 'common_key' , 'left' )
data_want.columns 

[< normal_columns>,common_key]

似乎都可以。

谁能解释这里发生了什么?此外,如何编写更长的版本,我发现这更熟悉。我似乎无法指向具有相同名称的第二列。

在带有Spark 3.2.1和Scala 2.12的Databrick上运行

Good day,

I'm joining data on pySpark. Coming from SQL I like to define join by common key like this.

data_want = data_1.join(data_2, data_1.common_key == data_2.common_key , 'left' )
data_want.columns 

[<normal_columns>, common_key, common_key]

I get doubly entries of common_key-column. Very odd. When doing this with shorter syntax:

data_want = data_1.join(data_2, 'common_key' , 'left' )
data_want.columns 

[<normal_columns>, common_key]

All seems to be ok.

Can anyone explain what's going on here? Moreover, how would one go about writing the longer version, which I find more familiar. I can't seem to point to 2nd column with same name.

Running on DataBricks with Spark 3.2.1 and Scala 2.12

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2025-02-14 18:26:42

在Spark中,每列都有Catalyst/SQL引擎使用的唯一ID。 ID是内部的,更多的是元数据,很难参考。加入表达式不会消除common_keys。因为他们有通用名称,所以他们加倍了。您不能通过称呼他们的名字来放下他们。 ID也被隐藏了。因此,一种常见的方法是介绍DF以创建一个唯一的名称。使用名称删除列。下面的代码。

另一个选择是重命名常见列,然后加入

data_1.alias('data_1').join(data_2, data_1.common_key == data_2.common_key , 'left' ).drop('data_1.common_key')

In spark, each column has a unique id used by the catalyst/sql engine. The id is internal and more of a meta data and very hard to refer to. Join expressions do not eliminate the common_keys. Because they have common names, they double up. You cant drop them by calling their names. The id is hidden too. A common approach is therefore to lias the df to create a unique name. use the name to drop the column. Code below.

Another option is to rename the common columns and then join

data_1.alias('data_1').join(data_2, data_1.common_key == data_2.common_key , 'left' ).drop('data_1.common_key')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文