pandas中按类型求占比

方法一

import pandas as pd
df = pd.DataFrame({'typeid': [9, 379, 65002001, 9, 379, 379, 379, 2],
                   'sl': [100, 200, 300, 400, 500, 600, 700, 80]})
df['sum'] = df['typeid'].map(lambda x : df.loc[df['typeid']==x,['sl']].sum().squeeze())
#添加一列sum,按typeid类型计算sl列的和,squeeze()取标量
df['ratio'] = df['sl']/df['sum']

方法二

def get_ratio(grouped,column):
    return(grouped[column]/grouped[column].sum())
df['ratio'] = df.groupby('typeid').apply(get_ratio,column='sl')

你可能感兴趣的:(pandas中按类型求占比)