pyspark:将多个列强制转换为数字

t30tvxxf  于 2021-05-26  发布在  Spark
关注(0)|答案(1)|浏览(267)

我正在应用一个方法,它给出了错误,因为演员没有做好
我如何才能1)以更有效的方式强制转换所有字段,2)仅使用withcolumn一次,然后3)使用数字(而不是字符串)运行方法:

q5 = q4.withColumn("DISTANCE", q4["LOCLAT"].cast(IntegerType()))
q6 = q4.withColumn("DISTANCE", q4["LOCLONG"].cast(IntegerType()))
q7 = q4.withColumn("DISTANCE", q4["LOCLAT2"].cast(IntegerType()))
q8 = q4.withColumn("DISTANCE", q4["LOCLONG2"].cast(IntegerType()))

q9 = (q4.withColumn('distance', haversine('LOCLONG', 'LOCLAT', 'LOCLONG2', 'LOCLAT2')))

谢谢!!

ufj5ltwl

ufj5ltwl1#

我不确定您想要实现什么,但下面是如何将所有4列转换为整数类型并调用 haversine 功能:

df = q4.select(
    '*',
    *[F.col(c).cast('int').alias(c + '_int')
      for c in ['LOCLONG', 'LOCLAT', 'LOCLONG2', 'LOCLAT2']]
)

df = df.withColumn(
    'distance',
    haversine('LOCLONG_int', 'LOCLAT_int', 'LOCLONG2_int', 'LOCLAT2_int')
)

相关问题