Spark Dataframe column with last character of other column

前端 未结 2 1483
囚心锁ツ
囚心锁ツ 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条回答
  • 2021-01-18 02:57

    Just use the substring function

    from pyspark.sql.functions import substring
    df.withColumn("b", substring(col("columnName"), -1, 1))
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题