如何在多个 when 条件下实现以下目标。
from pyspark.sql import functions as F
df = spark.createDataFrame([(5000, 'US'),(2500, 'IN'),(4500, 'AU'),(4500, 'NZ')],["Sales", "Region"])
df.withColumn('Commision',
F.when(F.col('Region')=='US',F.col('Sales')*0.05).\
F.when(F.col('Region')=='IN',F.col('Sales')*0.04).\
F.when(F.col('Region')in ('AU','NZ'),F.col('Sales')*0.04).\
otherwise(F.col('Sales'))).show()
最佳答案
在when
之后使用otherwise
:
df.withColumn('Commision',
F.when(F.col('Region') == 'US', F.col('Sales') * 0.05).otherwise(
F.when(F.col('Region') == 'IN', F.col('Sales') * 0.04).otherwise(
F.when(F.col('Region').isin('AU', 'NZ'), F.col('Sales') * 0.04).otherwise(
F.col('Sales'))))).show()
+-----+------+---------+
|Sales|Region|Commision|
+-----+------+---------+
| 5000| US| 250.0|
| 2500| IN| 100.0|
| 4500| AU| 180.0|
| 4500| NZ| 180.0|
+-----+------+---------+
关于PySpark DataFrame withColumn multiple when 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926454/