【发布时间】:2022-01-17 18:24:31
【问题描述】:
我正在尝试在 python 函数中使用 if 条件,然后使用它对数据框值进行一些计算。
#init data
+---+----+----+------+
| id|team|game|result|
+---+----+----+------+
| 1| A|Home| |
| 2| A|Away| |
| 3| B|Home| |
| 4| B|Away| |
| 5| C|Home| |
| 6| C|Away| |
| 7| D|Home| |
| 8| D|Away| |
+---+----+----+------+
### I wanna replace the value result and I tried use a function
def replace_result(team_name,game_kind,result):
if col('team') == team_name and col('game') == game_kind:
return result
else:
return col('result')
df = df.withColumn('result',replace_result('A','Away','0-1')
但给了我错误
ValueError:无法将列转换为布尔值:请使用 '&' 表示 'and'、'|'在构建 DataFrame 布尔表达式时,for 'or', '~' for 'not'。
我的问题是
是否可以使用 Pyspark 数据框列的 if 条件?
谢谢
【问题讨论】:
标签: python apache-spark pyspark