我有以下数据框,其中包含一列数组(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']]
用高阶函数rlike
函数检验58
的存在性。使用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 |
+---------------------------------------------------+-----+-----+