从由字符串数组 pyspark 或 python 高阶函数多个值组成的列中选择

发布于 2025-01-18 21:44:25 字数 507 浏览 3 评论 0原文

我有这样的表,我想根据列上列出的内容创建一个新列 示例

df.withcolumn('good',。何时('java'或'php'isin ['booksIntereste'])。lit(1).otherwise(0))

”

当包含java或Java时所需的输出php获得1 else 0 0

i have a table like this and i want to create a new column based on what is listed on the column
example

df.withcolumn('good',.when('java' or 'php' isin ['booksIntereste']).lit(1).otherwise(0))

data table

desired output when it contain java or php get 1 else 0
target

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

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

发布评论

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

评论(1

陪我终i 2025-01-25 21:44:25

您可以直接使用高阶函数 - array_contains 为此,另外您可以浏览 这篇文章了解更多

数据准备

d = {
    'name':['James','Washington','Robert','Micheal'],
    'booksInterested':[['Java','C#','Python'],[],['PHP','Java'],['Java']]
}

sparkDF = sql.createDataFrame(pd.DataFrame(d))

sparkDF.show()

+----------+------------------+
|      name|   booksInterested|
+----------+------------------+
|     James|[Java, C#, Python]|
|Washington|                []|
|    Robert|       [PHP, Java]|
|   Micheal|            [Java]|
+----------+------------------+

Array Contains

sparkDF = sparkDF.withColumn('good',F.array_contains(F.col('booksInterested'), 'Java'))

+----------+------------------+-----+
|      name|   booksInterested| good|
+----------+------------------+-----+
|     James|[Java, C#, Python]| true|
|Washington|                []|false|
|    Robert|       [PHP, Java]| true|
|   Micheal|            [Java]| true|
+----------+------------------+-----+

ForAll Array Contains - 多种的

sparkDF = sparkDF.withColumn('good_multiple',F.forall(F.col('booksInterested'), lambda x: x.isin(['Java','Python','PHP'])))

sparkDF.show()

+----------+------------------+-----+-------------+
|      name|   booksInterested| good|good_multiple|
+----------+------------------+-----+-------------+
|     James|[Java, C#, Python]| true|        false|
|Washington|                []|false|         true|
|    Robert|       [PHP, Java]| true|         true|
|   Micheal|            [Java]| true|         true|
+----------+------------------+-----+-------------+

You can directly use a Higher Order Function - array_contains for this , additionally you can browse through this article to understand more

Data Preparation

d = {
    'name':['James','Washington','Robert','Micheal'],
    'booksInterested':[['Java','C#','Python'],[],['PHP','Java'],['Java']]
}

sparkDF = sql.createDataFrame(pd.DataFrame(d))

sparkDF.show()

+----------+------------------+
|      name|   booksInterested|
+----------+------------------+
|     James|[Java, C#, Python]|
|Washington|                []|
|    Robert|       [PHP, Java]|
|   Micheal|            [Java]|
+----------+------------------+

Array Contains

sparkDF = sparkDF.withColumn('good',F.array_contains(F.col('booksInterested'), 'Java'))

+----------+------------------+-----+
|      name|   booksInterested| good|
+----------+------------------+-----+
|     James|[Java, C#, Python]| true|
|Washington|                []|false|
|    Robert|       [PHP, Java]| true|
|   Micheal|            [Java]| true|
+----------+------------------+-----+

ForAll Array Contains - Multiple

sparkDF = sparkDF.withColumn('good_multiple',F.forall(F.col('booksInterested'), lambda x: x.isin(['Java','Python','PHP'])))

sparkDF.show()

+----------+------------------+-----+-------------+
|      name|   booksInterested| good|good_multiple|
+----------+------------------+-----+-------------+
|     James|[Java, C#, Python]| true|        false|
|Washington|                []|false|         true|
|    Robert|       [PHP, Java]| true|         true|
|   Micheal|            [Java]| true|         true|
+----------+------------------+-----+-------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文