18

I have a column in a data frame in pyspark like “Col1” below. I would like to create a new column “Col2” with the length of each string from “Col1”. I’m new to pyspark, I’ve been googling but haven’t seen any examples of how to do this. Any tips are very much appreciated.

example:

Col1 Col2
12   2
123  3
1

1 Answer 1

43

You can use the length function:

import pyspark.sql.functions as F
df.withColumn('Col2', F.length('Col1')).show()
+----+----+
|Col1|Col2|
+----+----+
|  12|   2|
| 123|   3|
+----+----+

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.