In pyspark, how do you add/concat a string to a column?

前端 未结 2 892
生来不讨喜
生来不讨喜 2020-12-17 18:05

I would like to add a string to an existing column. For example, df[\'col1\'] has values as \'1\', \'2\', \'3\' etc and I would like to concat stri

2条回答
  •  感动是毒
    2020-12-17 19:01

    from pyspark.sql.functions import concat, col, lit
    
    
    df.select(concat(col("firstname"), lit(" "), col("lastname"))).show(5)
    +------------------------------+
    |concat(firstname,  , lastname)|
    +------------------------------+
    |                Emanuel Panton|
    |              Eloisa Cayouette|
    |                   Cathi Prins|
    |             Mitchel Mozdzierz|
    |               Angla Hartzheim|
    +------------------------------+
    only showing top 5 rows
    

    http://spark.apache.org/docs/2.0.0/api/python/pyspark.sql.html#module-pyspark.sql.functions

提交回复
热议问题