我有一个像
ID_0 ID_1 ID_2
0 a b 1
1 a c 1
2 a b 0
3 d c 0
4 a c 0
5 a c 1
Run Code Online (Sandbox Code Playgroud)
我想对 ['ID_0','ID_1'] 进行分组并生成一个新的数据帧,其中每个组的 ID_2 值的总和除以每个组中的行数。
grouped = df.groupby(['ID_0', 'ID_1'])
print grouped.agg({'ID_2': np.sum}), "\n", grouped.size()
Run Code Online (Sandbox Code Playgroud)
给
ID_2
ID_0 ID_1
a b 1
c 2
d c 0
ID_0 ID_1
a b 2
c 3
d c 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)
如何获得 np.sum 值除以 size() 值的新数据框?
Nic*_*eli 14
使用groupby.apply来代替:
df.groupby(['ID_0', 'ID_1']).apply(lambda x: x['ID_2'].sum()/len(x))
ID_0 ID_1
a b 0.500000
c 0.666667
d c 0.000000
dtype: float64
Run Code Online (Sandbox Code Playgroud)