cod*_*ing 3 python regex python-3.x apache-spark pyspark
我在数据框中有一列名为 "tags"。我需要根据条件提取值。条件是它不应该包含 _(Underscore) 和 :(Colon)。
例如:
“标签”:“嗨,你好,amount_10,amount_90,总计:100”
预期结果:
"new_column" : "嗨,你好"
供您参考:
我提取了所有金额标签
collectAmount = udf(lambda s: list(map(lambda amount: amount.split('_')[1] if len(collection) > 0
else amount, re.findall(r'(amount_\w+)', s))), ArrayType(StringType()))
productsDF = productsDF.withColumn('amount_tag', collectAmount('tags'))
Run Code Online (Sandbox Code Playgroud)
尝试这个
df.withColumn('new_column',expr('''concat_ws(',',array_remove(transform(split(tags,','), x -> regexp_extract(x,'^(?!.*_)(?!.*:).+$',0)),''))''')).show(2,False)
+-------------------------------------------+----------+
|tags |new_column|
+-------------------------------------------+----------+
|hai, hello, amount_10, amount_90, total:100|hai, hello|
|hai, hello, amount_10, amount_90, total:100|hai, hello|
+-------------------------------------------+----------+
Run Code Online (Sandbox Code Playgroud)