我有 Dataframe df ,列名称是如下文本,我想从中删除方括号 输入
[gh].[ijdnd]
[hyf]
dfvc.gfth
[]
[
]
输出
gh.ijnd
hyf
dfvc.gfth
null
null
null
让我们使用
regexp_replace
用空字符串替换方括号,然后用 null 屏蔽空值
r = F.regexp_replace('text', r'\[|\]', '')
r = F.when(r == '', F.lit(None)).otherwise(r)
result = df.withColumn('text', r)
+---------+
| text|
+---------+
| gh.ijdnd|
| hyf|
|dfvc.gfth|
| NULL|
| NULL|
| NULL|
+---------+