根据if-elif-else条件创建新列 [英] Creating a new column based on if-elif-else condition

查看:103
本文介绍了根据if-elif-else条件创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame df:

I have a DataFrame df:

    A    B
a   2    2 
b   3    1
c   1    3

我要根据以下条件创建一个新列:

I want to create a new column based on the following criteria:

如果行A == B: 0

如果行A > B: 1

如果行A < B: -1

因此,鉴于上表,它应该是:

so given the above table, it should be:

    A    B    C
a   2    2    0
b   3    1    1
c   1    3   -1 

对于典型的if else情况,我会做np.where(df.A > df.B, 1, -1),pandas是否提供一种特殊的语法来一步解决我的问题(无需创建3个新列,然后合并结果)?

For typical if else cases I do np.where(df.A > df.B, 1, -1), does pandas provide a special syntax for solving my problem with one step (without the necessity of creating 3 new columns and then combining the result)?

推荐答案

要正式化上面列出的一些方法:

To formalize some of the approaches laid out above:

创建一个对数据框的行进行操作的函数,如下所示:

Create a function that operates on the rows of your dataframe like so:

def f(row):
    if row['A'] == row['B']:
        val = 0
    elif row['A'] > row['B']:
        val = 1
    else:
        val = -1
    return val

然后通过axis=1选项将其应用于您的数据框:

Then apply it to your dataframe passing in the axis=1 option:

In [1]: df['C'] = df.apply(f, axis=1)

In [2]: df
Out[2]:
   A  B  C
a  2  2  0
b  3  1  1
c  1  3 -1

当然,这不是矢量化的,因此在扩展到大量记录时,性能可能会不佳.不过,我认为它更具可读性.尤其是来自SAS背景.

Of course, this is not vectorized so performance may not be as good when scaled to a large number of records. Still, I think it is much more readable. Especially coming from a SAS background.

这篇关于根据if-elif-else条件创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆