简体   繁体   English

熊猫如何根据其他列中的值汇总一列的总和

[英]pandas how to aggregate sum on a column depending on values in other columns

I am trying to sum values in a column by groupby on values in a second column, but meanwhile also considering values on a 3rd column, the df is like, 我试图对第二列中的值进行groupby ,但同时也考虑了第三列中的值, df就像,

id    memo    amount   
 1    pos     1.0 
 1    pos     2.0
 1    neg     3.0
 2    pos     4.0
 2    pos     5.0
 2    neg     6.0
 2    neg     7.0

I want to group by id and sum amount , but each group, if memo is pos it is positive and neg for negative, eg when groupby 1 , the total amount is 0, since -1.0 - 2.0 + 3.0 = 0 . 我想组由id和合计amount ,但每一组中,如果memopos它是正和neg负,例如,当groupby 1 ,总量为0时,由于-1.0 - 2.0 + 3.0 = 0

If I do df.groupby('id')['amount'].sum() , it only considers id and amount column, I am wondering how to also take memo into account here. 如果我执行df.groupby('id')['amount'].sum() ,则仅考虑idamount列,我想知道如何在此处也考虑memo

so the result will look like, 所以结果看起来像

id    memo    amount    total_amount   
 1    pos     1.0       0.0
 1    pos     2.0       0.0
 1    neg     3.0       0.0
 2    pos     4.0       -4.0
 2    pos     5.0       -4.0
 2    neg     6.0       -4.0
 2    neg     7.0       -4.0

Splitting the operation in two steps, you can achieve what you want through 分两步进行操作,即可实现所需的目标

df['temp'] = np.where(df.memo == 'pos', df.amount, -df.amount)
df['total_amount'] = df.groupby('id').temp.transform(sum)

Another fun way with mapping and multiplying ie 映射和乘法的另一种有趣方式,即

df['new'] = (df.set_index('id')['memo'].map({'pos':1,'neg':-1})*df['amount'].values)\
            .groupby(level=0).transform(sum).values

Output : 输出:

   id memo  amount  new
0   1  pos     1.0  0.0
1   1  pos     2.0  0.0
2   1  neg     3.0  0.0
3   2  pos     4.0 -4.0
4   2  pos     5.0 -4.0
5   2  neg     6.0 -4.0
6   2  neg     7.0 -4.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 pandas 中的其他列对一列的值求和? - How to sum values of one column based on other columns in pandas? Pandas:如何根据其他列值的条件对列求和? - Pandas: How to sum columns based on conditional of other column values? 根据不同列中的其他2个值汇总一列的值 - sum up values of a column depending on 2 other values ​from different columns 总和值取决于 python 中没有 pandas 的其他列 - sum values depending on other column without pandas in python Pandas:如何根据其他列值的条件创建对其他列求和的列? - Pandas: How create columns where sum other columns based on conditional of other column values? 根据其他列的条件评估熊猫列的值 - Assessing values ​to a pandas column with conditions depending on other columns 根据 pandas 的其他列在列中添加随机值 - Adding random values in column depending on other columns with pandas 根据其他两列的值,在 pandas 中创建一个新列 - Create a new column in pandas depending on values from two other columns Pandas 按年和月聚合并汇总其他列 - Pandas aggregate by year and month and sum other column 如何在熊猫中汇总总和并将唯一的行值转换为列名? - How to aggregate sum, and convert unique row values to column names, in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM