熊猫有条件地创建数据框列:基于最大多个条件

时间:2020-06-23 03:01:44

标签: python pandas numpy dataframe max

我有一个df:

  dog1  dog2  cat1  cat2  ant1  ant2
0    1     2     3     4     5     6
1    1     2     3     4     0     0
2    3     3     3     3     3     3
3    4     3     2     1     1     0

我要根据以下条件添加新列:

 if   max(dog1, dog2) > max(cat1, cat2) > max(ant1, ant2) ----->   2
 elif max(dog1, dog2) > max(cat1, cat2)                   ----->   1
 elif max(dog1, dog2) < max(cat1, cat2) < max(ant1, ant2) ----->  -2
 elif max(dog1, dog2) < max(cat1, cat2)                   ----->  -1
 else                                                     ----->   0

所以应该变成这样:

  dog1  dog2  cat1  cat2  ant1  ant2   new
0    1     2     3     4     5     6    -2
1    1     2     3     4     0     0    -1
2    3     3     3     3     3     3     0
3    4     3     2     1     1     0     2     

我知道如何使用straightforward condition来做到这一点,但是max。最好的方法是什么?

3 个答案:

答案 0 :(得分:1)

您可以在熊猫中使用.max(axis=1)函数:

conditions = [
       (df[['dog1','dog2']].max(axis=1) > df[['cat1','cat2']].max(axis=1)) & (df[['cat1','cat2']].max(axis=1) > df[['ant1','ant2']].max(axis=1)), 
       (df[['dog1','dog2']].max(axis=1) > df[['cat1','cat2']].max(axis=1)),
       (df[['dog1','dog2']].max(axis=1) < df[['cat1','cat2']].max(axis=1)) & (df[['cat1','cat2']].max(axis=1) < df[['ant1','ant2']].max(axis=1)), 
       (df[['dog1','dog2']].max(axis=1) < df[['cat1','cat2']].max(axis=1))]
choices = [2,1,-2,-1]
df['new'] = np.select(conditions, choices, default=0)

输出:

   dog1  dog2  cat1  cat2  ant1  ant2  new
0     1     2     3     4     5     6   -2
1     1     2     3     4     0     0   -1
2     3     3     3     3     3     3    0
3     4     3     2     1     1     0    2

答案 1 :(得分:1)

您可以使用apply Documentation

def newrow(dog1,dog2,cat1,cat2,ant1,ant2):
    if max(dog1, dog2) > max(cat1, cat2) > max(ant1, ant2):
        return 2
    elif max(dog1, dog2) > max(cat1, cat2):
        return 1
    elif max(dog1, dog2) < max(cat1, cat2) < max(ant1, ant2):
        return -2
    elif max(dog1, dog2) < max(cat1, cat2):
        return -1
    return 0

df['new'] = df.apply(lambda x: newrow(*x), axis=1)

新的df为

  dog1  dog2  cat1  cat2  ant1  ant2  new
0     1     2     3     4     5     6   -2
1     1     2     3     4     0     0   -1
2     3     3     3     3     3     3    0
3     4     3     2     1     1     0    2

答案 2 :(得分:0)

似乎您正在寻找np.maximum()。 尝试在numpy maximum上找到它 希望对您有帮助。