from
pyspark.sql.functions
import
trim, ltrim, rtrim
data
=
[(
1
,
"ABC "
), (
2
,
" DEF"
), (
3
,
" GHI "
)]
df
=
spark.createDataFrame(data
=
data, schema
=
[
"col1"
,
"col2"
])
df.show()
df.withColumn(
"col2"
, trim(
"col2"
)).show()
df.withColumn(
"col2"
, ltrim(
"col2"
)).show()
df.withColumn(
"col2"
, rtrim(
"col2"
)).show()
df.select(
"col1"
, trim(
"col2"
).alias(
'col2'
)).show()
df.createOrReplaceTempView(
"TAB"
)
spark.sql(
"select col1,trim(col2) as col2 from TAB"
).show()