将功能应用于数组中的所有元素< string>柱子

发布于 2025-02-05 21:28:17 字数 352 浏览 2 评论 0原文

我有一个带有字符串数组的列,例如:

["test.a" "random.ac"]
["test.41" "random.23" "test.123"]

我只想在“。”之前获得文字。我只为阵列的拳头元素做到了。我该如何处理所有元素?最好没有UDF。

df = df.withColumn("address", substring_index(col("all_addresses").getItem(0), ".", 1)))

I have a column with arrays of strings, e.g. like this:

["test.a" "random.ac"]
["test.41" "random.23" "test.123"]

I want to get only the text before the ".". I did it only for the fist element of the array. How do I do it to all elements? Preferably without UDFs.

df = df.withColumn("address", substring_index(col("all_addresses").getItem(0), ".", 1)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2025-02-12 21:28:18

我将使用与@wwnde的类似想法 - 变换函数。 变换采用数组,根据提供的功能转换其每个元素,并导致相同大小的数组,但带有更改的元素。正是您需要的。

但是,拥有相同的原始想法,我可能会以不同的方式实施。
2个选项:

from pyspark.sql import functions as F
df = spark.createDataFrame(
    [(["test.a", "random.ac"],),
     (["test.41", "random.23", "test.123"],)],
    ['c1']
)

df = df.withColumn('c2', F.transform('c1', lambda x: F.element_at(F.split(x, '\.'), 1)))
df = df.withColumn('c3', F.transform('c1', lambda x: F.regexp_extract(x, r'(.+)\.', 1)))

df.show()
# +--------------------+--------------------+--------------------+
# |                  c1|                  c2|                  c3|
# +--------------------+--------------------+--------------------+
# | [test.a, random.ac]|      [test, random]|      [test, random]|
# |[test.41, random....|[test, random, test]|[test, random, test]|
# +--------------------+--------------------+--------------------+

I would use a similar idea as @wwnde - transform function. transform takes an array, transforms every its element according to the provided function and results in the array of the same size, but with changed elements. Exactly what you need.

However, having the same original idea, I would probably implement it differently.
2 options:

from pyspark.sql import functions as F
df = spark.createDataFrame(
    [(["test.a", "random.ac"],),
     (["test.41", "random.23", "test.123"],)],
    ['c1']
)

df = df.withColumn('c2', F.transform('c1', lambda x: F.element_at(F.split(x, '\.'), 1)))
df = df.withColumn('c3', F.transform('c1', lambda x: F.regexp_extract(x, r'(.+)\.', 1)))

df.show()
# +--------------------+--------------------+--------------------+
# |                  c1|                  c2|                  c3|
# +--------------------+--------------------+--------------------+
# | [test.a, random.ac]|      [test, random]|      [test, random]|
# |[test.41, random....|[test, random, test]|[test, random, test]|
# +--------------------+--------------------+--------------------+
只涨不跌 2025-02-12 21:28:18

使用REGEXP_EXTRACT。 Extra Alphanumerics仅匹配时仅跟随特殊字符

+---+------------------------------+
|id |text                          |
+---+------------------------------+
|1  |[test.a, random.ac]           |
|2  |[test.41, random.23, test.123]|
+---+------------------------------+



 df.withColumn('new', expr("transform(text,x->regexp_extract(x,'[a-z0-9]+(?=\.)',0))")).show()

+---+--------------------+--------------------+
| id|                text|                 new|
+---+--------------------+--------------------+
|  1| [test.a, random.ac]|      [test, random]|
|  2|[test.41, random....|[test, random, test]|
+---+--------------------+--------------------+

Use regexp_extract. extra alphanumerics matches if only followed by special character .

+---+------------------------------+
|id |text                          |
+---+------------------------------+
|1  |[test.a, random.ac]           |
|2  |[test.41, random.23, test.123]|
+---+------------------------------+



 df.withColumn('new', expr("transform(text,x->regexp_extract(x,'[a-z0-9]+(?=\.)',0))")).show()

+---+--------------------+--------------------+
| id|                text|                 new|
+---+--------------------+--------------------+
|  1| [test.a, random.ac]|      [test, random]|
|  2|[test.41, random....|[test, random, test]|
+---+--------------------+--------------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文