【问题标题】:How to use multiple columns from a df to run multiple conditions to calculate new column? [duplicate]如何使用 df 中的多个列来运行多个条件来计算新列? [复制]
【发布时间】:2019-06-08 16:39:48
【问题描述】:

例如,假设我有一个df

a    b     c       

1    3     5       
5    9     4      

我有ifconditions:

if a < 2:
   3
elif  a < 3:
   4
else: b + c

如何测试条件并为我的 df 的每一行返回结果,如下所示?

a    b     c    d

1    3     5    3 
5    9     4    13

编辑:理想情况下,我想创建一个function,让我可以

def function(a, b, c) df['d'] = function(a, b, c)

并计算所有数据行。因为在实际数据中,有100+条条件语句和10s列。

【问题讨论】:

标签: python pandas


【解决方案1】:

你可以使用apply方法:

def custom_calc(x, a , b):
    if x[0] < 2:
        return 3

    if x[0] > 3:
        return x + a + b

df.a = df[['a', 'b', 'c']].apply(func=check, args=(df.b,df.c))

根据需要编辑自定义函数

【讨论】:

    【解决方案2】:

    您可以沿轴 1 使用apply 函数。

    def f(row):
        if row['a'] > 2:
            return 3
        elif row['a'] > 3:
            return 4
        else:
            return  row['b']+row['c']
    
    
    df.apply(f,axis=1)
    
    #output
    0    8
    1    3
    dtype: int64
    

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 2017-01-17
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      相关资源
      最近更新 更多