Используйте floor()
в десятичном формате года (year/10)
, а затем получите желаемый результат с помощью multiplying by 10
.
from pyspark.sql import functions as F
df.withColumn("decade", (F.floor(F.col("year")/10)*10).cast("int")).show()
#+----+------+
#|year|decade|
#+----+------+
#|2003| 2000|
#|2004| 2000|
#|2014| 2010|
#|2015| 2010|
#|2008| 2000|
#+----+------+
Мы также можем сделать это просто replacing the last number in year with 0
:
Используя concat and substring
:
from pyspark.sql import functions as F
df.withColumn("decade", F.expr("""concat(substring(year,1,length(year)-1),0)""")).show()
Использование regexp_replace
:
from pyspark.sql import functions as F
df.withColumn("decade", F.regexp_replace("year",'\d(?!.*\d)','0')).show()
Использование right
и subtract from year
:
from pyspark.sql import functions as F
df.withColumn("decade", F.expr("""int(year-right(year,1))""")).show()