简体   繁体   English

在 pyspark 中使用独立条件的时间和其他方式

[英]when and otherwise in pyspark using independent conditions

I am trying to make a new column based on list of items.我正在尝试根据项目列表创建一个新列。 cond is a separate independent list outside dataframe "ABC". cond 是数据框“ABC”之外的一个单独的独立列表。 A new column needs to be created based on few conditions, please see a dummy condition below:-需要根据几个条件创建一个新列,请参阅下面的虚拟条件:-

Rules = ['2'. '5', '18', '33', '47']

ABC = ABC.withColumn("XYZ", when ('2' in cond), col("sqr_4231"))\
                                   .when(('3' in cond), col("sqr_4999"))

but this is giving error - have tried lit('2') and lit('3') but still giving error.但这会出错 - tried lit('2')lit('3')但仍然出错。

To verify if a value contains in an array you need to use:要验证值是否包含在数组中,您需要使用:

lit('value').isin(array_value)
aggregate = aggregate.withColumn("ENR", when(lit('2').isin(Rules), col("sqr_4231"))\
                                   .when(lit('3').isin(Rules), col("sqr_4999"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM