如何在数据框中投射一列? [英] How to cast a column in dataframe?
本文介绍了如何在数据框中投射一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从 hbase 获取数据并将其转换为数据帧.现在,我在数据框中有一列 string
数据类型.但我需要将其数据类型转换为 Int
.
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:col should be column
我在这里给出了正确的列名,我需要在 pyspark 中更改上述代码的语法吗?
I have given proper column name here, Do i need to change the syntax of above mentioned code in pyspark?
推荐答案
要么:
df.withColumn("order", df.order.cast("int"))
或
from pyspark.sql.functions import expr
df.withColumn("order", expr("CAST(order AS INTEGER)"))
这篇关于如何在数据框中投射一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文