apache-spark - 在pyspark中将字符串列表转换为二进制列表

标签 apache-spark pyspark apache-spark-sql pyspark-dataframes

我有一个这样的数据框

data = [(("ID1", ['October', 'September', 'August'])), (("ID2", ['August', 'June', 'May'])), 
    (("ID3", ['October', 'June']))]
df = spark.createDataFrame(data, ["ID", "MonthList"])
df.show(truncate=False)

+---+----------------------------+
|ID |MonthList                   |
+---+----------------------------+
|ID1|[October, September, August]|
|ID2|[August, June, May]         |
|ID3|[October, June]             |
+---+----------------------------+

我想将每一行与默认列表进行比较,这样如果该值存在,则分配 1 else 0
default_month_list = ['October', 'September', 'August', 'July', 'June', 'May']

因此我的预期输出是这个
+---+----------------------------+------------------+
|ID |MonthList                   |Binary_MonthList  |
+---+----------------------------+------------------+
|ID1|[October, September, August]|[1, 1, 1, 0, 0, 0]|
|ID2|[August, June, May]         |[0, 0, 1, 0, 1, 1]|
|ID3|[October, June]             |[1, 0, 0, 0, 1, 0]|
+---+----------------------------+------------------+

我可以在 python 中做到这一点,但不知道如何在 pyspark 中做到这一点

最佳答案

你可以试试用这样的udf .

from pyspark.sql.functions import udf, col
from pyspark.sql.types import ArrayType, IntegerType

default_month_list = ['October', 'September', 'August', 'July', 'June', 'May']

def_month_list_func = udf(lambda x: [1 if i in x else 0 for i in default_month_list], ArrayType(IntegerType()))

df = df.withColumn("Binary_MonthList", def_month_list_func(col("MonthList")))

df.show()
# output
+---+--------------------+------------------+
| ID|           MonthList|  Binary_MonthList|
+---+--------------------+------------------+
|ID1|[October, Septemb...|[1, 1, 1, 0, 0, 0]|
|ID2| [August, June, May]|[0, 0, 1, 0, 1, 1]|
|ID3|     [October, June]|[1, 0, 0, 0, 1, 0]|
+---+--------------------+------------------+

关于apache-spark - 在pyspark中将字符串列表转换为二进制列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58303468/

相关文章:

python - 如何在 python 中使用 `map` 将 dict 值转换为整数?

python - PySpark 可以使用 numpy 数组吗?

python - 无法从 PySpark 将 Spark 数据帧保存到 Google Cloud Storage

apache-spark - 如何选择所有列而不是硬编码每一列?

scala - 如何在 Spark Streaming Scala 中对 HBase 进行单元测试

apache-spark - 按计数对collect_set进行排序

apache-spark - 如何将表转换为 Spark Dataframe

pyspark - 使用 EMR 上的 PySpark 连接来自不同 Glue 目录的表

java - Spark 地理瓷砖加入

scala - 使用 x.head、x.tail : _* in Spark 的原因