将功能应用于数组中的所有元素< string>柱子
我有一个带有字符串数组的列,例如:
["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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用与@wwnde的类似想法 -
变换
函数。变换
采用数组,根据提供的功能转换其每个元素,并导致相同大小的数组,但带有更改的元素。正是您需要的。但是,拥有相同的原始想法,我可能会以不同的方式实施。
2个选项:
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:
使用REGEXP_EXTRACT。 Extra
Alphanumerics
仅匹配时仅跟随特殊字符。
Use regexp_extract. extra
alphanumerics
matches if only followed by special character.