更改 PySpark 数据框中列的数据类型 [英] Change the Datatype of columns in PySpark dataframe
本文介绍了更改 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 的数据类型为 String
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
推荐答案
尝试使用 cast 方法:
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屋!
查看全文