Python 和熊猫在一起
我有这样的表格:Python 和熊猫在一起,python,pandas,Python,Pandas,我有这样的表格: Date Buyer_id 11.11.2016 1 11.11.2016 2 11.11.2016 2 13.12.2016 1 13.12.2016 3 13.12.2016 4 14.12.2016 3 14.12.2016 1 我需要计算一个数字 因此,我需要在不同的天访问商店的人数(这很重要)。 所以买家1,3是好的,买家2,4是坏的 结果应该是: 好买家=2 bad_buers=2这将产生您想要的结果: s = (df.groupby(
Date Buyer_id
11.11.2016 1
11.11.2016 2
11.11.2016 2
13.12.2016 1
13.12.2016 3
13.12.2016 4
14.12.2016 3
14.12.2016 1
我需要计算一个数字
因此,我需要在不同的天访问商店的人数(这很重要)。
所以买家1,3是好的,买家2,4是坏的
结果应该是:
好买家=2
bad_buers=2这将产生您想要的结果:
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
{'Bad': 2, 'Good': 2}
如果您需要结果字典:
d = s.to_dict()
print(d)
输出:
Good 2
Bad 2
Name: Date, dtype: int64
{'Bad': 2, 'Good': 2}