【笔记】Pandas分组聚合
agg函数
df_new = df_channel.groupby('日期').agg(
联系用户数=('联系次数', lambda x: len(x[x > 0])),
联系数=('联系商家数', 'sum'),
联系次数=('联系次数', 'sum'),
联系商家数=('联系商家数', 'nunique'),
).reset_index()
透视表
def nunique(x):
return len(set(x))
tab = pd.pivot_table(temp3,
index=['thedate','usercity','userid'],
columns=['property_id'],values=['cid'],
aggfunc=[nunique],fill_value=0
)['nunique']['cid'].reset_index()
tab
地振高冈,一派溪山千古秀;门朝大海,三合河水万年流。