How do I get the last item from a list using pyspark?

后端 未结 4 965
感情败类
感情败类 2021-01-12 03:04

Why does column 1st_from_end contain null:

from pyspark.sql.functions import split
df = sqlContext.createDataFrame([(\'a b c d\',)], [\'s\',])
d         


        
4条回答
  •  春和景丽
    2021-01-12 03:54

    Create your own udf would look like this

        def get_last_element(l):
            return l[-1]
        get_last_element_udf = F.udf(get_last_element)
    
        df.select(get_last_element(split(df.s, ' ')).alias('1st_from_end')
    

提交回复
热议问题