获取int()参数必须是字符串或数字,而不是'列' - Apache Spark

时间:2016-10-21 16:37:58

标签: python apache-spark pyspark

如果我使用以下代码,我将收到此异常:

int() argument must be a string or a number, not 'Column'
df= df.withColumn('FY',
    F.when((df['ID'].substr(5,2).isin({'11','12'})),int(df['ID'].substr(1,4))+1).
    otherwise(int(df['ID'].substr(1,4))))

基本上我想在结果为11或12的情况下为结果添加1,否则只需使用ID。请帮助,我是Python的新手。

1 个答案:

答案 0 :(得分:2)

使用:

df.withColumn('FY',F.when(df['ID'].substr(5,2).isin({'11','12'}),
  df['ID'].substr(1,4).cast("integer") + 1).
  otherwise(df['ID'].substr(1,4)).cast("integer"))