python-如何计算每年每月的交易

previous: https://stackoverflow.com/posts/61835927/

我有数据记录看起来像这样

category    dt          userid  amt
1           4/14/2019       1   140
1           5/1/2019        1   500
2           5/5/2019        1   300 
3           5/19/2019       1   230
2           6/17/2019       1   200
4           6/18/2019       1   400
1           7/30/2019       1   400
1           8/17/2019       1   300
2           12/2/2019       1   200
2           12/23/2019      1   500
1           1/10/2019       2   470
1           2/25/2019       2   450
2           10/4/2019       2   350

问题1:如何计算每月的交易次数(如果有=是,没有=否)

user    month1  month2  month3  month4  month5  month6  month7  month8  month9  month10 month11 month12 total_transaction_month
1       No      No      No      Yes     Yes     Yes     Yes     Yes     No      No      No      Yes      6
2       Yes     Yes     No      No      No      No      No      No      No      Yes     No      No       3

Q2:如何计算每个类别的交易

user   pro_cat1   pro_cat2  pro_cat3  pro_cat4    total_transaction_category
1      Yes        Yes       Yes       Yes         4
2      Yes        Yes       No        No          2
评论