如何使用 pySpark Dataframe 的多列创建 BinaryType 列? [英] How to create BinaryType Column using multiple columns of a pySpark Dataframe?

查看:49
本文介绍了如何使用 pySpark Dataframe 的多列创建 BinaryType 列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 pySpark,所以不知道关于这方面的很多细节.

I have recently started working with pySpark so don't know about many details regarding this.

我想在数据框中创建一个 BinaryType 列?但是很难做到...

I am trying to create a BinaryType column in a data frame? But struggling to do it...

例如,让我们以一个简单的 df

for example, let's take a simple df

df.show(2)

+---+----------+
|  col1|col2|
+---+----------+
|  "1"| null|
|  "2"| "20"|
+---+----------+

现在我想要像 BinaryType 这样的第三列col3"

Now I want to have a third column "col3" with BinaryType like

|  col1|col2| col3|
+---+----------+
|  "1"| null|[1 null]
|  "2"| "20"|[ 2 20]
+---+----------+

我该怎么做?

推荐答案

试试这个:

a = [('1', None), ('2', '20')]
df = spark.createDataFrame(a, ['col1', 'col2'])
df.show()

+----+----+
|col1|col2|
+----+----+
|   1|null|
|   2|  20|
+----+----+



df = df.withColumn('col3', F.array(['col1', 'col2']))
df.show()


+----+----+-------+
|col1|col2|   col3|
+----+----+-------+
|   1|null|   [1,]|
|   2|  20|[2, 20]|
+----+----+-------+

这篇关于如何使用 pySpark Dataframe 的多列创建 BinaryType 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆