Pandas group通过减法进行聚合

时间:2013-03-20 15:58:57

标签: python pandas

我有一个pandas数据框df,其中包含帐户条目,以便人名,帐户ID具有信用和借记条目,例如

date        Name      transaction-type  tran
2013-03-05  john Doe   credit          10
2013-05-05  john Doe   debit           20
2012-06-01  jane Doe   credit          50

我想按日期,名称和交易类型对交易进行分组,并汇总转账。我怎么能这样做?我希望能够在tran列上执行reduce(numpy.subtract),但我不确定Pandas的正确语法。

1 个答案:

答案 0 :(得分:1)

IIUC,您只需要.groupby然后.sum()

>>> df
                 date      Name transaction-type  tran
0 2013-03-05 00:00:00  john Doe           credit    10
1 2013-05-05 00:00:00  john Doe            debit    20
2 2012-06-01 00:00:00  jane Doe           credit    50
3 2012-06-01 00:00:00  jane Doe           credit    22
4 2012-06-02 00:00:00  jane Doe           credit    75
>>> df.groupby(["date", "Name", "transaction-type"]).sum()
                                      tran
date       Name     transaction-type      
2012-06-01 jane Doe credit              72
2012-06-02 jane Doe credit              75
2013-03-05 john Doe credit              10
2013-05-05 john Doe debit               20

请参阅文档中的groupby aggregation部分。

如果你想要总签名值,你也可以得到它:

>>> df["tran"][df["transaction-type"] == "debit"] *= -1
>>> df.groupby(["date", "Name"]).sum()
                     tran
date       Name          
2012-06-01 jane Doe    72
2012-06-02 jane Doe    75
2013-03-05 john Doe    10
2013-05-05 john Doe   -20