【问题标题】:How the replace value using regular expression如何使用正则表达式替换值
【发布时间】:2019-06-13 03:40:12
【问题描述】:

我有一个具有列名和速率的数据框,我想计算速率大于 4.0 的名称数

+--------------------+-----+
|                name| rate|
+--------------------+-----+
|               Jalsa|4.1/5|
|      Spice Elephant|4.1/5|
|     San Churro Cafe|3.8/5|
|Addhuri Udupi Bho...|3.7/5|
|       Grand Village|3.8/5|
+--------------------+-----+

【问题讨论】:

  • 想要的输出是什么?

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


【解决方案1】:

假设 df 是您的数据框,

from pyspark.sql import functions as F
# First, you filter your lines
df_filtered = df.where(F.split(F.col("rate"), '/').getItem(0).cast("double") > 4.0)

# Then, you count
df_filtered.count()
> 2

【讨论】:

    【解决方案2】:

    这个表达式会输出我们想要的数字,然后你可以简单地做数学:

    ([0-9.]+)\/([0-9]+)
    

    Demo

    测试

    # coding=utf8
    # the above tag defines encoding for this document and is for Python 2.x compatibility
    
    import re
    
    regex = r"([0-9.]+)\/([0-9]+)"
    
    test_str = ("+--------------------+-----+\n"
        "|                name| rate|\n"
        "+--------------------+-----+\n"
        "|               Jalsa|4.1/5|\n"
        "|      Spice Elephant|4.1/5|\n"
        "|     San Churro Cafe|3.8/5|\n"
        "|Addhuri Udupi Bho...|3.7/5|\n"
        "|       Grand Village|3.8/5|\n"
        "+--------------------+-----+\n")
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for matchNum, match in enumerate(matches, start=1):
    
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
    
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
    
    # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
    

    【讨论】:

    • 它不是需要的字符串上的正则表达式,而是数据帧上的火花函数。
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2012-02-12
    • 1970-01-01
    • 2018-08-03
    • 2021-11-19
    • 2019-01-24
    相关资源
    最近更新 更多