[英]Pandas group by subtraction on aggregation
I have a pandas dataframe df that has entries for account such that Person Name, Account id have credit and debit entries, for example 我有一个熊猫数据框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
I wanted to group the transactions by date, name and transaction-type and aggregate the tran?. 我想按日期,名称和交易类型对交易进行分组并汇总tran?。 How could I do this?
我该怎么办? I was hoping to be able to do a reduce(numpy.subtract) on the tran column but I am not really sure on the correct syntax for Pandas.
我希望能够在tran列上进行reduce(numpy.subtract),但是我不确定在Pandas的语法上是否正确。
IIUC, you simply want .groupby
and then .sum()
: 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
See the section on groupby aggregation in the docs. 请参阅文档中有关groupby聚合的部分。
If you want the total signed value, you could get that too: 如果您需要总签名值,也可以得到:
>>> 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.