用熊猫回购计数

时间:2017-02-21 10:27:39

标签: python pandas

我有这样的表:

[NonController]

我需要计算大熊猫回购号码 所以我需要在不同天访问商店的人数(这很重要)。 因此买家#1,3是好的,买家#2,4是坏的。

结果应该是: good_buyers = 2 bad_buers = 2

1 个答案:

答案 0 :(得分:1)

这会产生你想要的结果:

s = (df.groupby('Buyer_id')['Date']
         .apply(lambda x: 'Good' if len(x.unique()) > 1 else 'Bad')
         .value_counts())
print(s)

输出:

Good    2
Bad     2
Name: Date, dtype: int64

如果您需要结果字典:

d = s.to_dict()
print(d)

输出:

{'Bad': 2, 'Good': 2}