Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 熊猫基于另一列中选定为条件的值创建列_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 3.x 熊猫基于另一列中选定为条件的值创建列

Python 3.x 熊猫基于另一列中选定为条件的值创建列,python-3.x,pandas,dataframe,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

我有以下
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\u amount\u sum
,该列指示(布尔值)如果
amount
sum是我认为需要
groupby
by 2
系列
(列)而不是筛选:

df['0_amount_sum_'] = ((df.amount * np.where(df.negative_amount, -1, 1))
                           .groupby([df['id'], df['match_type']])
                           .transform('sum')
                           .le(0))

   id match_type  amount  negative_amount  0_amount_sum_
0   1      exact      10            False          False
1   1      exact      20            False          False
2   1       name      30            False          False
3   1       name      40            False          False
4   1     amount      15             True           True
5   1     amount      15             True           True
6   2      exact       0            False           True
7   2      exact       0            False           True

@ScottBoston因为
金额
对于
id=1
match_type=amount
,总和为-30,即

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))
df['0_amount_sum_'] = ((df.amount * np.where(df.negative_amount, -1, 1))
                           .groupby([df['id'], df['match_type']])
                           .transform('sum')
                           .le(0))

   id match_type  amount  negative_amount  0_amount_sum_
0   1      exact      10            False          False
1   1      exact      20            False          False
2   1       name      30            False          False
3   1       name      40            False          False
4   1     amount      15             True           True
5   1     amount      15             True           True
6   2      exact       0            False           True
7   2      exact       0            False           True