I have a dataframe that looks like below
temp = spark.createDataFrame([
(0, ['This','is','Spark']),
(1, ['I','wish','Java','could','use','case','classes']),
(2, ['Data','science','is','cool']),
(3, ['Machine','Learning'])
], ["id", "words"])
+---+------------------------------------------+
|id |words |
+---+------------------------------------------+
|0 |[This, is, Spark] |
|1 |[I, wish, Java, could, use, case, classes]|
|2 |[Data, science, is, cool] |
|3 |[Machine, Learning] |
+---+------------------------------------------+
I want to convert the above column words into lower case by keeping the structure in place.
The schema of the above dataframe is
|-- words: array (nullable = true)
| |-- element: string (containsNull = true)
I am applying an udf to convert the words into lower case
def lower(token):
return list(map(str.lower,token))
lower_udf = F.udf(lower)
df_mod1 = df_mod1.withColumn('token',lower_udf("words"))
After performing the above step my schema is changing. The token column is changing to string datatype from ArrayType()
|-- token: string (nullable = true)
I applied F.array() on the column, but it is enclosing on extra list
+---+--------------------------------------+
|id |words |
+---+--------------------------------------+
|0 |[[This, is, Spark]] |
|1 |[[I,wish,Java,could,use,case,classes]] |
|2 |[[Data,science,is,cool]] |
|3 |[[Machine,Learning]] |
+---+--------------------------------------+
Desired output:
+---+--------------------------------------+
|id |token |
+---+--------------------------------------+
|0 |[this, is, spark] |
|1 |[i,wish,java,could,use,case,classes] |
|2 |[data,science,is,cool] |
|3 |[machine,learning] |
+---+--------------------------------------+
|-- token: array (nullable = true)
| |-- element: string (containsNull = true)
How to convert the token type to array type after making to lower case?