close

How to convert column with string type to int form in pyspark data frame?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to convert column with string type to int form in pyspark data frame in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to convert column with string type to int form in pyspark data frame?

  1. How to convert column with string type to int form in pyspark data frame?

    You could use cast(as int) after replacing NaN with 0,
    data_df = df.withColumn("Plays", df.call_time.cast('float'))

  2. convert column with string type to int form in pyspark data frame

    You could use cast(as int) after replacing NaN with 0,
    data_df = df.withColumn("Plays", df.call_time.cast('float'))

Method 1

from pyspark.sql.types import IntegerType
data_df = data_df.withColumn("Plays", data_df["Plays"].cast(IntegerType()))
data_df = data_df.withColumn("drafts", data_df["drafts"].cast(IntegerType()))

You can run loop for each column but this is the simplest way to convert string column into integer.

Method 2

You could use cast(as int) after replacing NaN with 0,

data_df = df.withColumn("Plays", df.call_time.cast('float'))

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read