pandas根据另一列中的值创建一个列,该列选择作为条件

时间:2018-06-05 12:40:37

标签: python-3.x pandas dataframe

我有以下df

id    match_type    amount    negative_amount
1     exact         10        False
1     exact         20        False
1     name          30        False
1     name          40        False
1     amount        15        True
1     amount        15        True 
2     exact         0         False
2     exact         0         False

我想创建一个列0_amount_sum,指示(布尔值)amount和是< = 0,或者对于特定match_type的每个id是否为0。以下是结果df;

id    match_type    amount    0_amount_sum    negative_amount   
1     exact         10        False           False
1     exact         20        False           False
1     name          30        False           False
1     name          40        False           False
1     amount        15        True            True
1     amount        15        True            True
2     exact         0         True            False
2     exact         0         True            False

对于id=1match_type=exactamount总和为30,因此0_amount_sumFalse。代码如下,

df = df.loc[df.match_type=='exact']

df['0_amount_sum_'] = (df.assign(
    amount_n=df.amount * np.where(df.negative_amount, -1, 1)).groupby(
    'id')['amount_n'].transform(lambda x: sum(x) <= 0))

df = df.loc[df.match_type=='name']

df['0_amount_sum_'] = (df.assign(
    amount_n=df.amount * np.where(df.negative_amount, -1, 1)).groupby(
    'id')['amount_n'].transform(lambda x: sum(x) <= 0))

df = df.loc[df.match_type=='amount']

df['0_amount_sum_'] = (df.assign(
    amount_n=df.amount * np.where(df.negative_amount, -1, 1)).groupby(
    'id')['amount_n'].transform(lambda x: sum(x) <= 0))

我想知道是否有更好的方法/更高效的方法,特别是当match_type的值未知时,代码可以自动枚举所有可能的值,然后相应地进行计算。

1 个答案:

答案 0 :(得分:3)

我认为需要 2 conda info(列)而不是过滤:

groupby