How to cast a column in dataframe? [duplicate]

2019-06-15 00:20发布

问题:

This question already has an answer here:

  • how to change a Dataframe column from String type to Double type in pyspark 4 answers

I am taking data from hbase and converted it to dataframe. Now, I have a column in data-frame which is string datatype.But i need to convert its datatype to Int.

Tried below code but its throwing me an error

df.withColumn("order", 'order.cast(int)')

Error i am facing is below

error:col should be column

I have given proper column name here, Do i need to change the syntax of above mentioned code in pyspark?

回答1:

Either:

df.withColumn("order", df.order.cast("int"))

or

from pyspark.sql.functions import expr

df.withColumn("order", expr("CAST(order AS INTEGER)"))