小编ver*_*cla的帖子

根据另一列的元素从 pyspark 数组中删除元素

我想验证数组是否包含 Pyspark 中的字符串(Spark < 2.4)。

示例数据框:

column_1 <Array>           |    column_2 <String>
--------------------------------------------
["2345","98756","8794"]    |       8794
--------------------------------------------
["8756","45678","987563"]  |       1234
--------------------------------------------
["3475","8956","45678"]    |       3475
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我想比较两列column_1和column_2。如果column_1包含column_2,我应该从column_1中跳过它的值。我做了一个 udf 从column_1 中提取column_2,但不起作用:

def contains(x, y):
        try:
            sx, sy = set(x), set(y)
            if len(sx) == 0:
                return sx
            elif len(sy) == 0:
                return sx
            else:
                return sx - sy            
        # in exception, for example `x` or `y` is None (not a list)
        except:
            return sx
    udf_contains = udf(contains, 'string')
    new_df = my_df.withColumn('column_1', udf_contains(my_df.column_1, my_df.column_2))  
Run Code Online (Sandbox Code Playgroud)

预期结果: …

apache-spark apache-spark-sql pyspark

4
推荐指数
1
解决办法
5101
查看次数

如何使用分隔符连接 PySpark 中的多列?

我有一个pyspark Dataframe,我想加入 3 列。

id |  column_1   | column_2    | column_3
--------------------------------------------
1  |     12      |   34        |    67
--------------------------------------------
2  |     45      |   78        |    90
--------------------------------------------
3  |     23      |   93        |    56
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我想加入 3 列:column_1, column_2, column_3仅在其中添加一个值"-"

期待结果:

id |  column_1   | column_2    | column_3    |   column_join
-------------------------------------------------------------
1  |     12      |     34      |     67      |   12-34-67
-------------------------------------------------------------
2  |     45      |     78      |     90      |   45-78-90
-------------------------------------------------------------
3  |     23 …
Run Code Online (Sandbox Code Playgroud)

apache-spark apache-spark-sql pyspark

4
推荐指数
1
解决办法
3343
查看次数

如何在Number pyspark或NLP之间进行分配

我想在数据框的一栏中进行多次拆分。例:

s = "Cras mattis MP the -69661/69662;69663 /IS4567"
Run Code Online (Sandbox Code Playgroud)

我如何获得:

s = ['Cras', 'mattis', 'MP', 'the', '69661', '69662', '69663', 'IS4567' ]
Run Code Online (Sandbox Code Playgroud)

谢谢

nlp dataframe apache-spark pyspark

3
推荐指数
1
解决办法
116
查看次数

如何在 Array 上使用

我有一个 pyspark Dataframe,它包含 4 列。我想从一列中提取一些字符串,它的类型是Array of strings. 我使用了regexp_extract函数,但它返回了一个错误,因为regexp_extract它只接受一个字符串。

示例数据框:

id |  last_name | age | Identificator
------------------------------------------------------------------
12 | AA         | 23  |  "[""AZE","POI","76759","T86420","ADAPT"]"
------------------------------------------------------------------
24 | BB         | 24  | "[""SDN","34","35","AZE","21054","20126"]"
------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我想提取所有数字:

- contain 4, 5 or 6 digits
 - it should not attached to a letters.
 - if attached to letter Z ok, I should extract it.
 - save it in a new column in my Dataframe.
Run Code Online (Sandbox Code Playgroud)

我开始这样做,但它不起作用,因为标题是一个字符串数组。

expression = r'([0-9]){4,6}' …
Run Code Online (Sandbox Code Playgroud)

python pyspark

3
推荐指数
1
解决办法
462
查看次数

从数组元素中删除子字符串并复制 pyspark

我有一个 pyspark 数据框:

number  |  matricule      
--------------------------------------------
1       |  ["AZ 1234", "1234", "00100"]                   
--------------------------------------------
23      |  ["1010", "12987"]                   
--------------------------------------------
56      |  ["AZ 98989", "22222", "98989"]                   
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)

matricule数组中,如果我删除AZ字符串,我会有重复的值。我想删除"AZ"字符串然后删除matricule 数组中的重复值。知道有时我在 之后有一个空格AZ,我也应该将其删除。

我做了一个udf:

def remove_AZ(A)
    for item in A:
        if item.startswith('AZ'):
            item.replace('AZ','')
udf_remove_AZ = F.udf(remove_AZ)
df = df.withColumn("AZ_2", udf_remove_AZ(df.matricule))
Run Code Online (Sandbox Code Playgroud)

我在所有AZ_2列中都为空。

如何从matricule数组中的每个值中删除 AZ然后删除里面的重复项?谢谢

apache-spark pyspark pyspark-sql pyspark-dataframes

2
推荐指数
1
解决办法
549
查看次数

数组元素的总和取决于值条件 pyspark

我有一个 pyspark 数据框:

id   |   column
------------------------------
1    |  [0.2, 2, 3, 4, 3, 0.5]
------------------------------
2    |  [7, 0.3, 0.3, 8, 2,]
------------------------------
Run Code Online (Sandbox Code Playgroud)

我想创建一个 3 列:

  • Column 1: 包含元素之和 < 2
  • Column 2: 包含元素之和 > 2
  • Column 3: 包含元素的总和 = 2(有时我有重复的值,所以我计算它们的总和)如果我没有值,我将其设为 null。

期待结果:

id   |   column               |  column<2 |  column>2   | column=2 
------------------------------|--------------------------------------------  
1    |  [0.2, 2, 3, 4, 3, 0.5]|  [0.7]    |  [12]       |  null
---------------------------------------------------------------------------
2    |  [7, 0.3, 0.3, 8, 2,]  | [0.6] …
Run Code Online (Sandbox Code Playgroud)

apache-spark pyspark pyspark-sql pyspark-dataframes

1
推荐指数
1
解决办法
2471
查看次数

如何比较javascript中的数字

我有两个数字,每个数字都包含from 1 to 6 digits. 我想使用 JavaScript 对它们进行比较,并根据它们的匹配情况给出一个百分比。

比较应该从左到右进行。

例子:

if 6D matching then 100%  ==> Example: (value_1: 987456, value_2: 987456) 
if 5D matching then 90%   ==> Example: (value_1: 987450, value_2: 987456)
if 4D matching then 80%   ==> Example: (value_1: 987400, value_2: 987456)
if 3D matching then 60%   ==> Example: (value_1: 987000, value_2: 987456)
if 2D matching then 40%   ==> Example: (value_1: 980000, value_2: 987456)
else  0%           ==> Example: (value_1: 010101, value_2: 987456)
Run Code Online (Sandbox Code Playgroud)

我希望我很清楚。你能给我建议一个解决方案我怎样才能在函数中做到这一点?谢谢

javascript

1
推荐指数
1
解决办法
133
查看次数