【问题标题】:Pandas DataFrame - How to apply Lambda Function on multiple columns and create a new columnPandas DataFrame - 如何在多列上应用 Lambda 函数并创建新列
【发布时间】:2021-02-11 09:17:00
【问题描述】:

Pandas DataFrame = df(例子)如下:

----------------------------------
   col_1   col_2   col_3   col_4  ... etc.
----------------------------------
0  34.91   12.45    0.00  256.95
1   0.00    0.00    0.00    0.00
2   2.34  346.78    1.23    0.02
3   0.00   78.95   36.78    2.95
4   0.03   46.21  128.05   30.00
5   0.05    0.10    0.07    0.05
----------------------------------
df = df.assign(col_new_bool = lambda x: True if   ((x['col_1'] < 0.0001) and 
                                                   (x['col_2'] < 0.0002) and 
                                                   (x['col_3'] < 0.0003) and 
                                                   (x['col_3'] < 0.0004)) 
                                             else False)

我想在数据框 df 中创建一个新列(名为 new_col_bool)。

如果所有 4 列都为零,

new_col_bool 将包含布尔值 True

new_col_bool 将包含布尔值 False 如果 4 列中的任何一列不为零。

请帮助正确的 lambda 函数?

注意:
df 有 100 多列,但我的 new_col_bool 仅基于 4 列计算。
** 如何检查这 4 列中的每一列的不同阈值?**

【问题讨论】:

    标签: pandas dataframe lambda


    【解决方案1】:

    您不需要 lambda 函数来处理琐碎的事情,使用 DataFrame.all 而不是 axis=1

    df['new_col_bool'] = df.eq(0).all(axis=1)
    
       col_1   col_2   col_3   col_4  new_col_bool
    0  34.91   12.45    0.00  256.95         False
    1   0.00    0.00    0.00    0.00          True
    2   2.34  346.78    1.23    0.02         False
    3   0.00   78.95   36.78    2.95         False
    4   0.03   46.21  128.05   30.00         False
    5   0.05    0.10    0.07    0.05         False
    

    要只检查某些列,请先选择这些:

    cols = ['col_1', 'col_2', 'col_3', 'col_4']
    df['new_col_bool'] = df[cols].eq(0).all(axis=1)
    
       col_1   col_2   col_3   col_4  new_col_bool
    0  34.91   12.45    0.00  256.95         False
    1   0.00    0.00    0.00    0.00          True
    2   2.34  346.78    1.23    0.02         False
    3   0.00   78.95   36.78    2.95         False
    4   0.03   46.21  128.05   30.00         False
    5   0.05    0.10    0.07    0.05         False
    

    检查任何情况:

    cols = ['col_1', 'col_2', 'col_3', 'col_4']
    cond = df[cols] > 0.5
    # or cond = df[cols] <= -1.3
    df['new_col_bool'] = cond.all(axis=1)
    

    【讨论】:

    • 我的 df 有 100 多列,我只需要检查 4 列。我该怎么做?
    • 如何检查这 4 列中每一列的不同阈值?感谢您的及时答复 - 不胜感激!
    • .eq(0) 代表“等于0”,可以使用任意条件,见编辑
    【解决方案2】:

    我认为转置数据帧并求和会很有效:

    df['new_col_bool'] = df.T.sum() == 0
    df
    Out[1]: 
       col_1   col_2   col_3   col_4  new_col_bool
    0  34.91   12.45    0.00  256.95         False
    1   0.00    0.00    0.00    0.00          True
    2   2.34  346.78    1.23    0.02         False
    3   0.00   78.95   36.78    2.95         False
    4   0.03   46.21  128.05   30.00         False
    

    或者对于特定的列:

    df['new_col_bool'] = df.T.iloc[0:4].sum() == 0
    df
    Out[1]: 
       col_1   col_2   col_3   col_4  new_col_bool
    0  34.91   12.45    0.00  256.95         False
    1   0.00    0.00    0.00    0.00          True
    2   2.34  346.78    1.23    0.02         False
    3   0.00   78.95   36.78    2.95         False
    4   0.03   46.21  128.05   30.00         False
    

    要按阈值执行,请使用max

    df['new_col_bool'] = df.T.iloc[0:4].max() < 100
    df
    Out[1]: 
       col_1   col_2   col_3   col_4  new_col_bool
    0  34.91   12.45    0.00  256.95         False
    1   0.00    0.00    0.00    0.00          True
    2   2.34  346.78    1.23    0.02         False
    3   0.00   78.95   36.78    2.95          True
    4   0.03   46.21  128.05   30.00         False
    5   0.05    0.10    0.07    0.05          True
    

    【讨论】:

    • 如何检查这 4 列中每一列的不同阈值?
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 2021-12-13
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2021-03-04
    • 2016-01-07
    相关资源
    最近更新 更多