[英]How to use when and Otherwise statement for a Spark dataframe by boolean columns?
I have a dataset with three columns, col 1: country (String), col 2: threshold_1 (bool), col 3: threshold_2 (bool)我有一个包含三列的数据集,col 1:country(String),col 2:threshold_1(bool),col 3:threshold_2(bool)
I am trying to create a new column with this logic, but getting an error我正在尝试使用此逻辑创建一个新列,但出现错误
I am using the Palantir code workbook for this, can anyone tell me what I am missing here?我正在为此使用 Palantir 代码工作簿,谁能告诉我我在这里缺少什么?
df = df.withColumn("Threshold_Filter",
when(df["country"]=="INDIA" & df["threshold_1"]==True | df["threshold_2 "]==True, "Ind_country"
).otherwise("Dif_country"))
You just need to put your statements in parentheses.你只需要把你的陈述放在括号里。
df = (
df
.withColumn(
"Threshold_Filter",
when(
(df["country"]=="INDIA") &
(df["threshold_1"]==True) |
(df["threshold_2 "]==True),
"Ind_country")
.otherwise("Dif_country"))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.