我有275列,我想在列中搜索25正则表达式字符串"^D(410|412)。如果此搜索字符串出现在25 列中的任何一列中,我想添加true到MyNewColumn.
使用下面我可以为2列做到这一点。是否有传递可变列数的方法?
下面的代码适用于 2 列
def moreThanTwoArgs(col1,col2):
return bool((re.search("^D(410|412)",col1) or re.search("^D(410|412)",col2)))
twoUDF= udf(moreThanTwoArgs,BooleanType())
df = df.withColumn("MyNewColumn", twoUDF(df["X1"], df["X2"]))
Run Code Online (Sandbox Code Playgroud)