Typecast Integer to Decimal and Integer to float in Pyspark

In order to typecast an integer to decimal in pyspark we will be using cast() function with DecimalType() as argument, To typecast integer to float in pyspark we will be using cast() function with FloatType() as argument. Let’s see an example of type conversion or casting of integer column to decimal column and integer column to float column in pyspark.

  • Type cast an integer column to decimal column in pyspark
  • Type cast an integer column to float column in pyspark

We will be using the dataframe named df_cust

Typecast Integer to Decimal and Integer to float in Pyspark 1

 

Typecast an integer column to string column in pyspark:

First let’s get the datatype of zip column as shown below

### Get datatype of zip column

df_cust.select("zip").dtypes

so the resultant data type of zip column is integer

Typecast Integer to Decimal and Integer to float in Pyspark 2

Now let’s convert the zip column to string using cast() function with DecimalType() passed as an argument which converts the integer column to decimal column in pyspark and it is stored  as a dataframe named output_df

########## Type cast an integer column to Decimal column in pyspark

from pyspark.sql.types import DecimalType
output_df = df_cust.withColumn("zip",df_cust["zip"].cast(DecimalType()))

 Now let’s get the datatype of zip column as shown below

### Get datatype of zip column

output_df.select("zip").dtypes

so the resultant data type of zip column is decimal

Typecast Integer to Decimal and Integer to float in Pyspark 3

 

 

 

 

Typecast an integer column to float column in pyspark:

First let’s get the datatype of zip column as shown below

### Get datatype of zip column

df_cust.select("zip").dtypes

so the resultant data type of zip column is integer

Typecast Integer to Decimal and Integer to float in Pyspark 4

Now let’s convert the zip column to string using cast() function with FloatType() passed as an argument which converts the integer column to float column in pyspark and it is stored  as a dataframe named output_df

########## Type cast integer column to float column in pyspark

from pyspark.sql.types import FloatType
output_df = df_cust.withColumn("zip",df_cust["zip"].cast(FloatType()))

Now let’s get the datatype of zip column as shown below

### Get datatype of zip column

output_df.select("zip").dtypes

So the resultant data type of zip column is float

Typecast Integer to Decimal and Integer to float in Pyspark 5

 


Other Related Topics:

 

Typecast Integer to Decimal and Integer to float in Pyspark                                                                                              

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts