Pandas groupby each column and add new column for each group

拟墨画扇 提交于 2019-12-06 03:38:37

Use list comprehension:

cols = ['lvl1','lvl2']
k = ['{}_avg'.format(x) for x in cols]
df = df.join(pd.concat([df.groupby(c)['wgt'].transform('mean') for c in cols], 1, keys=k))
print (df)
  lvl1 lvl2   wgt  lvl1_avg  lvl2_avg
0  l1A  l2A  0.20      0.25  0.216667
1  l1A  l2A  0.30      0.25  0.216667
2  l1B  l2A  0.15      0.15  0.216667
3  l1C  l26  0.05      0.05  0.050000
4  l1D  l27  0.30      0.30  0.300000
l=[]
l.append(df)
for x ,y in  enumerate(df.columns[:-1]):
    l.append(df.groupby(y).transform('mean').add_suffix('_{}1avg'.format(x+1)))
pd.concat(l,1)
Out[1328]: 
  lvl1 lvl2   wgt  wgt_11avg  wgt_21avg
0  l1A  l2A  0.20       0.25   0.216667
1  l1A  l2A  0.30       0.25   0.216667
2  l1B  l2A  0.15       0.15   0.216667
3  l1C  l26  0.05       0.05   0.050000
4  l1D  l27  0.30       0.30   0.300000

Here is a non-pandas solution. From the resulting dictionary, it's possible to efficiently map to columns.

from collections import defaultdict
import pandas as pd

df = pd.DataFrame([['l1A', 'l2A', 0.20],
                   ['l1A', 'l2A', 0.30],
                   ['l1B', 'l2A', 0.15],
                   ['l1C', 'l26', 0.05],
                   ['l1D', 'l27', 0.30]],
                  columns=['lvl1', 'lvl2', 'wgt'])

results = defaultdict(lambda: defaultdict(float))
arr = df.values

for i in range(1, 3):
    for x in sorted(np.unique(arr[:, i-1])):
        results[i][x] = np.mean(arr[np.where(arr[:, i-1]==x)][:, 2])
    df['avg_lvl'+str(i)] = df['lvl'+str(i)].map(results[i])

#   lvl1 lvl2   wgt  avg_lvl1 avg_lvl2
# 0  l1A  l2A  0.20     0.25  0.216667
# 1  l1A  l2A  0.30     0.25  0.216667
# 2  l1B  l2A  0.15     0.15  0.216667
# 3  l1C  l26  0.05     0.05  0.050000
# 4  l1D  l27  0.30     0.30  0.300000

For this miniature dataset I see the following performance for 3 responses:

%timeit pandas1(df)  # wen
# 10 loops, best of 3: 35 ms per loop

%timeit pandas2(df)  # jezrael
# 100 loops, best of 3: 4.54 ms per loop

%timeit numpy1(df)   # jp_data_analysis
# 1000 loops, best of 3: 1.88 ms per loop
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!