【笔记】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
posted @ 2021-05-31 17:55  介个车车烫屁股  阅读(73)  评论(0编辑  收藏  举报