更改PySpark数据框中的列的数据类型 [英] Change the Datatype of columns in PySpark dataframe

查看:1347
本文介绍了更改PySpark数据框中的列的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入数据框( ip_df ),该数据框中的数据如下所示:

I have an input dataframe(ip_df), data in this dataframe looks like as below:

id            col_value
1               10
2               11
3               12

id和col_value的数据类型为字符串

Data type of id and col_value is String

我需要获取另一个数据帧( output_df ),其id的数据类型为字符串,而col_value列的数据类型为十进制**(15,4)**.这不是数据转换,只是数据类型转换. 我可以通过PySpark使用它吗?任何帮助将不胜感激

I need to get another dataframe(output_df), having datatype of id as string and col_value column as decimal**(15,4)**. THere is no data transformation, just data type conversion. Can i use it using PySpark. Any help will be appreciated

推荐答案

尝试使用

Try using the cast method:

from pyspark.sql.types import DecimalType
<your code>
output_df = ip_df.withColumn("col_value",ip_df["col_value"].cast(DecimalType()))

这篇关于更改PySpark数据框中的列的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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