pyspark数据框将多列转换为浮点数 [英] Pyspark dataframe convert multiple columns to float

查看:271
本文介绍了pyspark数据框将多列转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像这样将数据帧的多列从字符串转换为浮点数

I am trying to convert multiple columns of a dataframe from string to float like this

df_temp = sc.parallelize([("1", "2", "3.4555"), ("5.6", "6.7", "7.8")]).toDF(("x", "y", "z"))
df_temp.select(*(float(col(c)).alias(c) for c in df_temp.columns)).show()

但是我得到了错误

select() argument after * must be a sequence, not generator

我不明白为什么会引发此错误

I cannot understand why this error is being thrown

推荐答案

float()不是Spark函数,您需要函数cast():

float() is not a Spark function, you need the function cast():

from pyspark.sql.functions import col
df_temp.select(*(col(c).cast("float").alias(c) for c in df_temp.columns))

这篇关于pyspark数据框将多列转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆