[英]pandas create a column based on values in another column which selected as conditions
I have the following df
, 我有以下
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
I want to create a column 0_amount_sum
that indicates (boolean) if the amount
sum is <= 0 or not for each id of a particular match_type
, eg the following is the result df
; 我想创建一个列
0_amount_sum
,指示(boolean)如果对于特定match_type
每个id, amount
和<= 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
for id=1
and match_type=exact
, the amount
sum is 30, so 0_amount_sum
is False
. 对于
id=1
和match_type=exact
, amount
总和为30,因此0_amount_sum
为False
。 The code is as follows, 代码如下,
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))
I am wondering if there is a better way/more efficient to do that, especially when the values of match_type
is unknown, so the code can automatically enumerate all the possible values and then do the calculation accordingly. 我想知道是否有更好的方法/更高效的方法,特别是当
match_type
的值未知时,代码可以自动枚举所有可能的值,然后相应地进行计算。
I believe need groupby
by 2 Series
(columns) instead filtering: 我相信需要
groupby
by 2 Series
(列)而不是过滤:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.