如何在pyspark中添加新列,并将其值基于其他列?

问题描述 投票:0回答:1

我的数据集如下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

python pyspark unsupervised-learning
1个回答
0
投票

要将值与提供的列表进行比较,需要使用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是您要比较的值列表。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.