在两个条件下在熊猫的Sumifs

问题描述 投票:1回答:3

我希望大熊猫相当于Excel的sumifs

=SUMIFS($D4:D$107,$D$107,$G4:G$107)

我有三个列,contractamounttransaction_type_tla。对于每个contract,如果交易类型是amount,我想总结CBP。以下公式无效:

data['Var']=(data.groupby('contract',"transaction_type_tla=='CBP'")['amount'].cumsum())
python pandas pandas-groupby
3个回答
1
投票

借用jp'data :-)

df['New']=df.groupby('contract').apply(lambda x : x['amount'][x['type']=='CBP'].cumsum()).reset_index(level=0,drop=True)
df
Out[258]: 
  contract  amount type    New
0        A     123  ABC    NaN
1        A     341  ABC    NaN
2        A     652  CBP  652.0
3        A     150  CBP  802.0
4        B     562  DEF    NaN
5        B     674  ABC    NaN
6        B     562  CBP  562.0
7        B     147  CBP  709.0

0
投票

这是一种方式。我已经设置了一些想要测试的虚数据。

输出是相同格式的数据帧,但CBP事务总和。

import pandas as pd

df = pd.DataFrame([['A', 123, 'ABC'],
                   ['A', 341, 'ABC'],
                   ['A', 652, 'CBP'],
                   ['A', 150, 'CBP'],
                   ['B', 562, 'DEF'],
                   ['B', 674, 'ABC'],
                   ['B', 562, 'CBP'],
                   ['B', 147, 'CBP']],
                  columns=['contract', 'amount', 'type'])

s = df.groupby(['contract', 'type'])['amount'].sum()
df = df.set_index(['contract', 'type']).join(s, rsuffix='_group')

df.loc[pd.IndexSlice[:, 'CBP'], 'amount'] = df.loc[pd.IndexSlice[:, 'CBP'], 'amount_group']
df = df.drop('amount_group', 1).reset_index().drop_duplicates()

#   contract type  amount
# 0        A  ABC     123
# 1        A  ABC     341
# 2        A  CBP     802
# 4        B  ABC     674
# 5        B  CBP     709
# 7        B  DEF     562

0
投票

编辑:我认为@Wen的回答更符合您的要求,但如果您想将结果作为一个系列:

一种简单的方法是首先通过您正在寻找的transaction_type_tla过滤事务列表,然后应用groupby和您想要的任何聚合方法:

ans = data[data['transaction_type_tla'] == 'CBP']
ans.groupby('contract')['amount'].cumsum()

这将导致您的答案系列化。

© www.soinside.com 2019 - 2024. All rights reserved.