当列表值与Pyspark数据帧中的列值的子字符串匹配时,填充新列

问题描述 投票:2回答:2

我在Pyspark有一个数据框,如下所示

df.show()

+---+----------------------+
| id|                   con|
+---+----------------------+
|  3|           mac,mac pro|
|  1|        iphone5,iphone|
|  1| android,android phone|
|  1|    windows,windows pc|
|  1| spy camera,spy camera|
|  2|               camera,|
|  3|             cctv,cctv|
|  2|   apple iphone,iphone|
|  3|           ,spy camera|
+---+----------------------+

我想基于某些lists创建新列。列表如下

phone_list = ['iphone', 'android', 'nokia']
pc_list = ['windows', 'mac']

Condition:

if a element in a list matches a string/substring in a column then flag the column to the value of that particular list

基本上我想要的是在phone_list我有元素iphone所以应该匹配id 1其中coniphone5, iphone和旗帜为phones等等。

Expected result

+---+----------------------+------+----+
| id|                   con|   cat| abc|
+---+----------------------+------+----+
|  3|           mac,mac pro|  null|  pc|
|  1|        iphone5,iphone|phones|null|
|  1| android,android phone|phones|null|
|  1|    windows,windows pc|  null|  pc|
|  1| spy camera,spy camera|  null|null|
|  2|               camera,|  null|null|
|  3|             cctv,cctv|  null|null|
|  2|   apple iphone,iphone|phones|null|
|  3|           ,spy camera|  null|null|
+---+----------------------+------+----+

我在下面做了。

df1 = df.withColumn('cat', F.when(df.con.isin(phone_list), 'phones')).withColumn('abc', F.when(df.con.isin(pc_list), 'pc'))

output

df1.show()

+---+----------------------+----+----+
| id|                   con| cat| abc|
+---+----------------------+----+----+
|  3|           mac,mac pro|null|null|
|  1|        iphone5,iphone|null|null|
|  1| android,android phone|null|null|
|  1|    windows,windows pc|null|null|
|  1| spy camera,spy camera|null|null|
|  2|               camera,|null|null|
|  3|             cctv,cctv|null|null|
|  2|   apple iphone,iphone|null|null|
|  3|           ,spy camera|null|null|
+---+----------------------+----+----+

我怎样才能以正确的方式进行这种比较?

python apache-spark pyspark
2个回答
4
投票

最好的方法是避免使用udf并使用pyspark.sql.Column.rlike()。如果列与参数中包含的正则表达式匹配,则返回True

在这种情况下,您可以使用"|".join(list_of_terms)创建一个匹配列表中任何单词的正则表达式模式。 ("|"OR运营商)

from pyspark.sql.functions import col, when

df.select(
    "*", 
    when(col("con").rlike("|".join(phone_list)), "phones").alias("cat"), 
    when(col("con").rlike("|".join(pc_list)), "pc").alias("abc")
).show(truncate=False)
#+---+---------------------+------+----+
#|id |con                  |cat   |abc |
#+---+---------------------+------+----+
#|3  |mac,mac pro          |null  |pc  |
#|1  |iphone5,iphone       |phones|null|
#|1  |android,android phone|phones|null|
#|1  |windows,windows pc   |null  |pc  |
#|1  |spy camera,spy camera|null  |null|
#|2  |camera,              |null  |null|
#|3  |cctv,cctv            |null  |null|
#|2  |apple iphone,iphone  |phones|null|
#|3  |,spy camera          |null  |null|
#+---+---------------------+------+----+

如果没有指定pyspark.sql.functions.when()条件,我们也使用null将返回otherwise()的事实。


2
投票

定义udf函数以检查con列中包含的字符串列表应该可以获得所需的输出

phone_list = ['iphone', 'android', 'nokia']
pc_list = ['windows', 'mac']

from pyspark.sql import functions as f
from pyspark.sql import types as t
def checkIsIn(con):
    phones = None
    pc = None
    for x in phone_list:
        if(x in con):
            phones = 'phones'
    for x in pc_list:
        if x in con:
            pc = 'pc'
    return (phones, pc)

checkIsInUdf = f.udf(checkIsIn, t.StructType([t.StructField('cat', t.StringType(), True), t.StructField('abc', t.StringType(), True)]))

df.withColumn('temp', checkIsInUdf(f.col('con')))\
    .select(f.col('id'), f.col('con'), f.col('temp.*'))\
    .show(truncate=False)

哪个应该给你

+---+---------------------+------+----+
|id |con                  |cat   |abc |
+---+---------------------+------+----+
|3  |mac,mac pro          |null  |pc  |
|1  |iphone5,iphone       |phones|null|
|1  |android,android phone|phones|null|
|1  |windows,windows pc   |null  |pc  |
|1  |spy camera,spy camera|null  |null|
|2  |camera,              |null  |null|
|3  |cctv,cctv            |null  |null|
|2  |apple iphone,iphone  |phones|null|
|3  |,spy camera          |null  |null|
+---+---------------------+------+----+

我希望答案是有帮助的

© www.soinside.com 2019 - 2024. All rights reserved.