如何在数据框中投射一列?

Ahi*_*ito -2 dataframe apache-spark apache-spark-sql pyspark

我正在从 hbase 获取数据并将其转换为数据帧。现在,我在数据框中有一列是string数据类型。但我需要将其数据类型转换为Int.

尝试了下面的代码,但它给我一个错误

df.withColumn("order", 'order.cast(int)')
Run Code Online (Sandbox Code Playgroud)

我面临的错误如下

error:col should be column
Run Code Online (Sandbox Code Playgroud)

我在这里给出了正确的列名,我需要在 pyspark 中更改上述代码的语法吗?

小智 5

任何一个:

df.withColumn("order", df.order.cast("int"))
Run Code Online (Sandbox Code Playgroud)

或者

from pyspark.sql.functions import expr

df.withColumn("order", expr("CAST(order AS INTEGER)"))
Run Code Online (Sandbox Code Playgroud)