Spark Dataframe column with last character of other column

前端 未结 2 1482
囚心锁ツ
囚心锁ツ 2021-01-18 02:43

I\'m looking for a way to get the last character from a string in a dataframe column and place it into another column.

I have a Spark dataframe that looks like this:

2条回答
  •  -上瘾入骨i
    2021-01-18 03:03

    One way is by using Column substr() function:

    df = df.withColumn("lastchar", df.animal.substr(-1,1))

    See documentation: https://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html#pyspark.sql.Column.substr

提交回复
热议问题