在 pandas 中使用groupby时如何分别对负值和正值求和? [英] How to sum negative and positive values separately when using groupby in pandas?

查看:109
本文介绍了在 pandas 中使用groupby时如何分别对负值和正值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在pandas中以不同的方式将正值和负值求和并将它们放在positivenegative列中?

How to sum positive and negative values differently in pandas and put them let's say in positive and negative columns?

我有这个数据框,如下所示:

I have this dataframe like below:

df = pandas.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
                   'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
                   'C' : np.random.randn(8), 'D' : np.random.randn(8)})

输出如下:

df
     A      B         C         D
0  foo    one  0.374156  0.319699
1  bar    one -0.356339 -0.629649
2  foo    two -0.390243 -1.387909
3  bar  three -0.783435 -0.959699
4  foo    two -1.268622 -0.250871
5  bar    two -2.302525 -1.295991
6  foo    one -0.968840  1.247675
7  foo  three  0.482845  1.004697

我使用下面的代码得到底片:

I used the below code to get negatives:

df['negative'] = df.groupby('A')['C'].apply(lambda x: x[x<0].sum()).reset_index()]

但是问题是,当我要将其添加到名为negativedataframe列之一时,它会显示错误:

But the problem is when I want to add it to one of dataframe columns called negative it gives error:

ValueError: Wrong number of items passed 2, placement implies 1

同样,我知道它说groupby返回了多列并且不能将其分配给df['negatives'],但是我不知道如何解决问题的这一部分.我也需要有一个积极的上校.

Again I know what it says that groupby has returned more than one column and cannot assign it to df['negatives'] but I don't know how to solve this part of the problem. I need to have positive col too.

期望的结果将是:

    A      Positive   Negative
0  foo     0.374156  -0.319699
1  bar     0.356339  -0.629649

解决该问题的正确方法是什么?

What is the right solution to the problem?

推荐答案

In [14]:
df.groupby(df['A'])['C'].agg([('negative' , lambda x : x[x < 0].sum()) , ('positive' , lambda x : x[x > 0].sum())])
Out[14]:
     negative   positive
A       
bar -1.418788   2.603452
foo -0.504695   2.880512

这篇关于在 pandas 中使用groupby时如何分别对负值和正值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆