简体   繁体   中英

How do I write case statements in Pyspark using Palantir Foundry

Trying to take a case statement from macros in VBA and transfer into Pyspark. How would I go about writing something like this in Pyspark?

 Select Case dMinutes
                Case 0 To 30
                    xnum = 1
                Case 31 To 60
                    xnum = 2
                Case 61 To 90
                    xnum = 3
                Case 91 To 120
                    xnum = 4
                Case Else
                    xnum = 5

The following PySpark should achieve what you want!

import pyspark.sql.functions as F

dataset = dataset.withColumn('xnum',
    F.when(F.col('dMinutes').between(0,30), 1)
    .when(F.col('dMinutes').between(31,60), 2)
    .when(F.col('dMinutes').between(61,90), 3)
    .when(F.col('dMinutes').between(91,120), 4)
    .otherwise(5)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM