pyspark:基于子字符串获得数组元素的索引

发布于 2025-01-27 06:24:28 字数 702 浏览 2 评论 0原文

我有以下数据框,其中包含数组列(col1)。我需要获取包含某个子字符串的元素索引(“ 58 =”)。

+-----------------------------------------------------------+-----+
|                                                      col1 |a_pos|
+-----------------------------------------------------------+-----+
|[8=FIX.4.4, 55=ITUBD264, 58=AID[43e39b2e-c6e2-4947]        |    0|
+-----------------------------------------------------------+-----+

我尝试使用array_position(col1,“ 58 =”),但看来它仅适用于确切的匹配,而不是子字符串。

在Python中,我正是这样做的,但是在Pandas中,使用以下代码:

df['idx'] = [max(range(len(l)), key=lambda x: '58=' in l[x]) for l in df['col1']]

I have the following dataframe, that contains a column of arrays (col1). I need to get the index of the element that contains a certain substring ("58=").

+-----------------------------------------------------------+-----+
|                                                      col1 |a_pos|
+-----------------------------------------------------------+-----+
|[8=FIX.4.4, 55=ITUBD264, 58=AID[43e39b2e-c6e2-4947]        |    0|
+-----------------------------------------------------------+-----+

I've tried to use array_position(col1, "58="), but it seems it only works with the exact match and not substrings.

In Python i'm doing exactly this, but in pandas, by using the following code:

df['idx'] = [max(range(len(l)), key=lambda x: '58=' in l[x]) for l in df['col1']]

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

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

发布评论

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

评论(1

风柔一江水 2025-02-03 06:24:28

使用58使用rlike功能在高阶功能中检查。使用array_position确定位置。下面的代码

df = df.withColumn('index',expr("array_position(transform(col1, x-> rlike(x,58)),true)")).show(truncate=False)
+---------------------------------------------------+-----+-----+
|col1                                               |a_pos|index|
+---------------------------------------------------+-----+-----+
|[8=FIX.4.4, 55=ITUBD264, 58=AID[43e39b2e-c6e2-4947]|0    |3    |
+---------------------------------------------------+-----+-----+

Check existence of 58 using the rlike function in a higher order function. Determine position using array_position. Code below

df = df.withColumn('index',expr("array_position(transform(col1, x-> rlike(x,58)),true)")).show(truncate=False)
+---------------------------------------------------+-----+-----+
|col1                                               |a_pos|index|
+---------------------------------------------------+-----+-----+
|[8=FIX.4.4, 55=ITUBD264, 58=AID[43e39b2e-c6e2-4947]|0    |3    |
+---------------------------------------------------+-----+-----+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文