cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Length Value of a column in pyspark

RohiniMathur
New Contributor II

Hello,

i am using pyspark 2.12

After Creating Dataframe can we measure the length value for each row.

For Example: I am measuring length of a value in column 2

Input file

|TYCO|1303|

|EMC |120989|

|VOLVO|102329|

|BMW|130157|

|FORD|004|

Output in Dataframe i am trying to get like

|TYCO|1303|4

|EMC |120989|6

|VOLVO|1023295|7

|BMW|130157|6

|FORD|004| 3

Please suggest if it is possible.

1 ACCEPTED SOLUTION

Accepted Solutions

lee
Contributor

You can use the length function for this

from pyspark.sql.functions import length
mock_data = [('TYCO', '1303'),('EMC', '120989'), ('VOLVO', '102329'),('BMW', '130157'),('FORD', '004')]
df = spark.createDataFrame(mock_data, ['col1', 'col2'])
df2 = df.withColumn('length_col2', length(df.col2))

Published notebook to show full example:

https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/3249...

View solution in original post

1 REPLY 1

lee
Contributor

You can use the length function for this

from pyspark.sql.functions import length
mock_data = [('TYCO', '1303'),('EMC', '120989'), ('VOLVO', '102329'),('BMW', '130157'),('FORD', '004')]
df = spark.createDataFrame(mock_data, ['col1', 'col2'])
df2 = df.withColumn('length_col2', length(df.col2))

Published notebook to show full example:

https://databricks-prod-cloudfront.cloud.databricks.com/public/4027ec902e239c93eaaa8714f173bcfc/3249...

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group