计算分组中计数器增加的次数

时间:2019-03-05 10:02:15

标签: python pandas

我有一个熊猫数据框,看起来像:

df = pd.DataFrame(data={'id':[1234, 1234, 1234, 1234, 1234], 'year':['2017', '2017', '2018', '2018', '2018'], 'count_to_today':[1, 2, 3, 3, 4})

我需要计算每年count_to_today累积发生id的次数。即

counts = pd.DataFrame(data={'id':[1234, 1234, 1234, 1234, 1234], 'year':['2017', '2017', '2018', '2018', '2018'], 'count_to_today':[1, 2, 1, 1, 2]})

即自开始以来,我一直在进行计数,因此我想计算它每年累计增加的次数。

我对如何执行此操作感到有些困惑。我知道我需要对idyear进行分组,但是我不知道如何获取.count().value_counts()来给我每年的计数。

1 个答案:

答案 0 :(得分:1)

与您之前的问题类似,但是使用// Required models const InvestorModel = require("mongoose").model("Investor"); const deletedInvestor = InvestorModel.remove({ _id }); const deletedInvestor = InvestorModel.deleteOne({ _id }); const deletedInvestor = InvestorModel.findByIdAndRemove(_id); const deletedInvestor = InvestorModel.findOneAndRemove({_id}); const deletedInvestor = InvestorModel.findByIdAndDelete(_id); const deletedInvestor = InvestorModel.findOneAndDelete({_id});

cumsum

df.count_to_today.diff().ne(0).groupby([df.id, df.year]).cumsum()

0    1.0
1    2.0
2    1.0
3    1.0
4    2.0
Name: count_to_today, dtype: float64