Get String length of column in Pyspark

In order to get string length of column in pyspark we will be using length() Function. We look at an example on how to get string length of the column in pyspark.

  • Get string length of the column in pyspark using length() function.

Syntax:

 length(“colname”)

colname –  column name

We will be using the dataframe named df_booksGet String length in Pyspark 1

 

 

Get String length of column in Pyspark:

In order to get string length of the column in pyspark we will be using length() function. which takes up the column name as argument and returns length


### Get String length of the column in pyspark

import pyspark.sql.functions as F

df = df_books.withColumn("length_of_book_name", F.length("book_name"))
df.show(truncate=False)

So the resultant dataframe with length of the column appended to the dataframe will be

Get String length in Pyspark 2

 

Get String length of column in Pyspark                                                                                               Get String length of column in Pyspark