【问题标题】:add a new column to a dataframe that will indicate if another column contains a word pyspark向数据框中添加一个新列,该列将指示另一列是否包含单词 pyspark
【发布时间】:2020-10-10 18:41:13
【问题描述】:

我有一个数据框,我想向它添加一个列,该列将指示单词“yes”是否在该行文本列中(如果单词在该行中,则为 1,如果不是,则为 0) 仅当“是”作为单词而不是子字符串出现时,我才需要检查 1 或者如果“是”在标点符号旁边(例如:是!) 我怎么能在火花中做到这一点? 例如:

id  group  text
1   a       hey there
2   c       no you can
3   a       yes yes yes
4   b       yes or no
5   b       you need to say yes.
6   a       yes you can
7   d       yes!
8   c       no&
9   b       ok

结果将是:

id  group  text                  check
1   a       hey there             0
2   c       no you can            0
3   a       yes yes yes           1
4   b       yes or no             1
5   b       you need to say yes.  1
6   a       yes you can           1
7   d       yes!                  1
8   c       no&                   0
9   b       ok                    0

【问题讨论】:

  • 您想要纯火花解决方案,还是 SQL 解决方案适合您?
  • 如果可以的话,都可以

标签: python sql dataframe pyspark apache-spark-sql


【解决方案1】:

您可以使用rlike 检查并转换为整数:

import pyspark.sql.functions as F
df.withColumn("check",F.col("text").rlike("yes").cast("Integer")).show()

+---+-----+--------------------+-----+
| id|group|                text|check|
+---+-----+--------------------+-----+
|  1|    a|           hey there|    0|
|  2|    c|          no you can|    0|
|  3|    a|         yes yes yes|    1|
|  4|    b|           yes or no|    1|
|  5|    b|you need to say yes.|    1|
|  6|    a|         yes you can|    1|
|  7|    d|                yes!|    1|
|  8|    c|                 no&|    0|
|  9|    b|                  ok|    0|
+---+-----+--------------------+-----+

对于已编辑的问题,您可以尝试使用higher order functions

import string
import re
pat = '|'.join([re.escape(i) for i in list(string.punctuation)])

(df.withColumn("text1",F.regexp_replace(F.col("text"),pat,""))
.withColumn("Split",F.split("text1"," "))
.withColumn("check",
  F.expr('''exists(Split,x-> replace(x,"","") = "yes")''').cast("Integer"))
.drop("Split","text1")).show()

+---+-----+--------------------+-----+
| id|group|                text|check|
+---+-----+--------------------+-----+
|  1|    a|           hey there|    0|
|  2|    c|          no you can|    0|
|  3|    a|         yes yes yes|    1|
|  4|    b|           yes or no|    1|
|  5|    b|you need to say yes.|    1|
|  6|    a|         yes you can|    1|
|  7|    d|                yes!|    1|
|  8|    c|                 no&|    0|
|  9|    b|               okyes|    0|
+---+-----+--------------------+-----+

【讨论】:

  • 只有当“yes”作为单词而不是子字符串出现或者“yes”出现在标点符号旁边(例如:yes!)时,我才需要勾选 1你的答案?
  • @shreder1921 虽然在回答问题后更新问题是一种不好的做法,但如果 spark 版本为 2.4+,您可以尝试我编辑的答案(第二部分),检查最后一行是否有更改
【解决方案2】:

只有当“yes”作为单词而不是子字符串出现时,我才需要检查1

您可以通过将text 与使用单词边界 (\b) 的正则表达式进行匹配来解决此问题。这是一个方便的正则表达式功能,表示分隔单词的字符(空格、标点符号等)。

在 SQL 中,你会这样做:

select
    t.*
    case when text rlike '\byes\b' then 1 else 0 end as check
from mytable t

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多