我有一个列为字符串的数据框。 我想在PySpark中将列类型更改为Double类型。
以下是我所做的方法:
toDoublefunc = UserDefinedFunction(lambda x: x,DoubleType())
changedTypedf = joindf.withColumn("label",toDoublefunc(joindf['show']))
我只是想知道,这样做是否正确,因为在运行逻辑回归时,我遇到了一些错误,所以我想知道,这是问题的原因吗。
我有一个列为字符串的数据框。 我想在PySpark中将列类型更改为Double类型。
以下是我所做的方法:
toDoublefunc = UserDefinedFunction(lambda x: x,DoubleType())
changedTypedf = joindf.withColumn("label",toDoublefunc(joindf['show']))
我只是想知道,这样做是否正确,因为在运行逻辑回归时,我遇到了一些错误,所以我想知道,这是问题的原因吗。
Column
已经提供了 cast
方法,其带有 DataType
实例:from pyspark.sql.types import DoubleType
changedTypedf = joindf.withColumn("label", joindf["show"].cast(DoubleType()))
或短字符串:
changedTypedf = joindf.withColumn("label", joindf["show"].cast("double"))
规范字符串名称(也可以支持其他变体)对应于simpleString
值。因此,对于原子类型:
from pyspark.sql import types
for t in ['BinaryType', 'BooleanType', 'ByteType', 'DateType',
'DecimalType', 'DoubleType', 'FloatType', 'IntegerType',
'LongType', 'ShortType', 'StringType', 'TimestampType']:
print(f"{t}: {getattr(types, t)().simpleString()}")
BinaryType: binary
BooleanType: boolean
ByteType: tinyint
DateType: date
DecimalType: decimal(10,0)
DoubleType: double
FloatType: float
IntegerType: int
LongType: bigint
ShortType: smallint
StringType: string
TimestampType: timestamp
比如说复杂类型
types.ArrayType(types.IntegerType()).simpleString()
'array<int>'
types.MapType(types.StringType(), types.IntegerType()).simpleString()
'map<string,int>'
通过使用与输入列相同的名称,保留列名并避免添加额外的列:
from pyspark.sql.types import DoubleType
changedTypedf = joindf.withColumn("show", joindf["show"].cast(DoubleType()))
from pyspark.sql.types import IntegerType
for ftr in ftr_list:
df = df.withColumn(f, df[f].cast(IntegerType()))
- Quetzalcoatlf
是什么?你在哪里使用了ftr
? - Sheldore给出的答案足以解决问题,但我想分享另一种方法,可能会在 Spark 的新版本中引入(我不确定),因此给出的答案没有涵盖它。
我们可以使用col("colum_name")
关键字在 Spark 语句中访问列:
from pyspark.sql.functions import col
changedTypedf = joindf.withColumn("show", col("show").cast("double"))
'double'
比 DoubleType()
更优雅,后者可能还需要导入。 - ZygDPySpark版本:
df = <source data>
df.printSchema()
from pyspark.sql.types import *
# Change column type
df_new = df.withColumn("myColumn", df["myColumn"].cast(IntegerType()))
df_new.printSchema()
df_new.select("myColumn").show()
toDoublefunc = UserDefinedFunction(lambda x: float(x),DoubleType())
changedTypedf = joindf.withColumn("label",toDoublefunc(joindf['show']))
使用:
df1.select(col('show').cast("Float").alias('label')).show()
或者
df1.selectExpr("cast(show AS FLOAT) as label").show()
其他答案存在一个问题(取决于您的Pyspark版本),即使用withColumn
。至少在v2.4.4中观察到了性能问题(请参见此thread)。spark docs关于withColumn
的说明如下:
此方法在内部引入了投影。因此,多次调用它,例如通过循环添加多个列,可能会生成大型计划,从而导致性能问题甚至StackOverflowException。为避免这种情况,请一次使用多个列进行选择。
通常实现建议使用select
的一种方法是:
from pyspark.sql.types import *
from pyspark.sql import functions as F
cols_to_fix = ['show']
other_cols = [col for col in joindf.columns if not col in cols_to_fix]
joindf = joindf.select(
*other_cols,
F.col('show').cast(DoubleType())
)
col
函数同样有效。from pyspark.sql.functions import col
,changedTypedf = joindf.withColumn("label", col("show").cast(DoubleType()))
- Staza