简体   繁体   English

如何在 python 中按月计算收入?

[英]How to calculate revenue by month between years in python?

The columns in the below dataset will represent:以下数据集中的列将代表:

A: Date contract opened; A: 合约开仓日期;

B: Date contract stops; B:合约停止日期;

C: Unique account ID against which contract associated (can have multiple contracts live against one ID) C:与合约关联的唯一账户 ID(可以有多个合约针对一个 ID 生效)

D: Monthly revenue for contract period - for simplicity, assume revenue generated from first month contract assumed up to month before the date the contract closes D:合同期间的每月收入——为简单起见,假设从第一个月合同产生的收入假设到合同结束前一个月

Start Date  contract end date   Unique Account Field            MRR
1/2/2013         1/2/2015                 50e55                 195.00
1/2/2013         1/2/2014                 4ee75                 50.00
1/2/2013         1/2/2014                 4f031                 75.00
1/2/2013         1/2/2016                 4c3b2                 133.00
1/2/2013         1/2/2016                 49ec8                 132.00
1/3/2013         1/3/2014                 49fc8                 59.00
1/4/2013         1/4/2015                 49wc8                 87.00
12/27/2013       12/27/2014               50bf7                 190.00
12/27/2013       12/27/2014               49cc8                 179.00
12/27/2013       12/27/2014               49wc8                 147.00
etc....

I would like to calculate the following:我想计算以下内容:

1.How much revenue was generated by month between Jan-2013 and Dec-2014? 1. 2013 年 1 月至 2014 年 12 月期间每月产生了多少收入?

2.How many active contracts (generated revenue in that month) were there by month between Jan-2013 and Dec-2014? 2. 2013 年 1 月至 2014 年 12 月期间,按月有多少活跃合约(当月产生的收入)?

3.How many active accounts (generated revenue from at least one contract) were there by month between Jan-2013 and Dec-2014? 3. 2013 年 1 月至 2014 年 12 月期间,每月有多少活跃账户(从至少一份合同产生的收入)?

I tried the below code: I was able to use sum() to get the revenues, but I'm not sure what to do beyond this.我尝试了下面的代码:我能够使用 sum() 来获得收入,但我不确定除此之外还能做什么。

from datetime import date
df['date'] = pd.to_datetime(df['Start Date'])    
df.groupby(df['Start Date'].dt.strftime('%B'))['MRR'].sum().sort_values() 

Result I got from the above code:我从上面的代码得到的结果:

Start Date
February     221744
January      241268
July         245811
August       247413
April        249702
March        251219
June         251494
May          259149
September    263395
October      293990
November     296590
December     311659

I need to calculate the above following.我需要计算以下内容。 How can I achieve this in python?如何在 python 中实现这一点?

Maybe you want something like this?也许你想要这样的东西?

date_range = (df['date'] >= "2013-01-01") & (df['date'] <= "2014-12-31")
df[date_range].groupby(df['date'].dt.strftime('%B')).agg(
    MRR=('MRR', 'sum'),
    Contracts=('date', 'count'),
    Accounts=('Unique Account Field', 'nunique')
)

Output:- Output:-

            MRR   Contracts Accounts
date            
December    516.0  3        3
January     731.0  7        7

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM