【问题标题】:How to write reduce with lambda function for pyspark data-frame?如何使用 lambda 函数为 pyspark 数据帧编写 reduce?
【发布时间】:2020-08-20 14:14:35
【问题描述】:

我想用 lambda 实现 reduce 函数。例如,如果有 pyspark 数据框并且在某个列上,我想获取那些列值长度在某个范围之间的所有记录。如何使用带有 lambda 的 reduce 函数来实现这一点。 我尝试用下面的代码来实现它,但我不太确定。

bad_df = reduce(lambda x, (min < f.col(x) < max for x in df.columns if x in col_list))

col_list 有我要检查列值是否在范围内的列列表。

【问题讨论】:

    标签: python dataframe lambda pyspark


    【解决方案1】:

    根据问题和方法,我了解到您想验证col_list 中的所有列是否在(min, max) 中都有值。

    解决方案

    
    total = df.count
    ans = True
    
    for col_name in col_list:
        
        #check if all values of that column lie within the range
        match_count = df.where(min < col(col_name) < max).count
    
        
        ans = count == match_count
        
        #if not then no need to go further
        if not ans:
             break
    
    
    

    【讨论】:

    • 我想用reduce和lambda函数来解决这个问题。你提供的解决方案,我能够为这个问题编写程序。
    • Lambda 减少在这里效率低下,如果 col_list 中的第一列不符合条件,那么你应该停在那里。 Lambda reduce 将评估所有内容并最终返回 false(不短路)。
    猜你喜欢
    • 1970-01-01
    • 2016-12-04
    • 2018-09-11
    • 2017-05-09
    • 2023-04-03
    • 1970-01-01
    • 2011-07-03
    • 2019-12-24
    • 2015-05-29
    相关资源
    最近更新 更多