You have to use the ColName
variable inside the function, not the string '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')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…