I have an input dataframe(ip_df), data in this dataframe looks like as below:
id col_value
1 10
2 11
3 12
Data type of id and col_value is String
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()))
neeraj bhadani
try below statement.
output_df = ip_df.withColumn("col_value",ip_df["col_value"].cast('float'))
来源:https://stackoverflow.com/questions/45453294/change-the-datatype-of-columns-in-pyspark-dataframe