我的数据集如下nogk is a data frame with mutliple columns。现在我想创建一个新列filed_position,该列仅基于position取3个值。the new column can only take 3 values defender,forward or middle。什么是解决的最佳方法。我应该使用以下代码还是循环通过位置code i used ,but i need to get "DEF"for all values which are in DEFENDER list。
要将值与提供的列表进行比较,需要使用isin函数
nogk=nogk.withColumn('Field Position',F.when((F.col('Position').isin(DEFENDER),'DEF').otherwise(
F.when((F.col('Position').isin(FORWARD),'FWD').otherwise(
F.when((F.col('Position').isin(MIDDLE),'MID').otherwise(F.lit('0'))
其中DEFENDER,FORWARD和MIDDLE是您要比较的值列表。
希望有帮助。