[英]How to define a function whose input is a column name?
我想定义一个有 2 个输入的 function。 数据集和列名。 不幸的是,我无法传递列名,因为我收到错误:
def def(df,ColName):
F.instr('ColName', s)) for s in stop_words]),
# If no stop words found, get the whole string
F.length('ColName') + 1)
).selectExpr('trim(substring(ColName, 1, idx-1)) ColName')
return Transitions
当我调用 function 时:
Transitions= Collecting_transition(Originaltitles,originaltitle)
我得到错误:
名称“原始标题”未定义
我试图传递字符串originaltitle
但又因为这部分).selectExpr('trim(substring(ColName, 1, idx-1)) ColName')
我得到另一个错误。
您必须使用 function 中的ColName
变量,而不是字符串'ColName'
。
def fun(df, ColName):
Transitions = df.withColumn('idx',
F.coalesce(
# Get the smallest index of a stop word in the string
F.least(*[F.when(F.instr(ColName, s) != 0, F.instr(ColName, s)) for s in stop_words]),
# If no stop words found, get the whole string
F.length(ColName) + 1)
).selectExpr(f'trim(substring({ColName}, 1, idx-1)) {ColName}')
return Transitions
Transitions = fun(Originaltitles, 'originaltitle')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.