您可以使用以下方法 在组内 获得 归一化的权重
transform:
>>> df['weight'] = df['dist'] / df.groupby('ind')['dist'].transform('sum')>>> df['weight']0 0.3571431 0.4166672 0.2500003 0.2857144 0.5833335 0.2857146 0.7142867 0.107143Name: weight, dtype: float64
>>> df['wcas'], df['wdiff'] = (df[n] * df['weight'] for n in ('cas', 'diff'))>>> df.groupby('ind')[['wcas', 'wdiff']].sum() wcas wdiffind g 6.714286 2.785714la 3.107143 4.882143p 3.750000 2.558333
编辑:就地突变:
>>> backup = df.copy() # make a backup copy to mutate in place>>> cols = df.columns[:2] # cas, diff>>> df[cols] = df['weight'].values[:, None] * df[cols]>>> df.groupby('ind')[cols].sum() cas diffind g 6.714286 2.785714la 3.107143 4.882143p 3.750000 2.558333
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)