df.groupby(['key1','key2'])[['data2']].mean()
Out[27]:
data2
key1 key2
a one 0.072958
two -0.962946
b one 0.946299
two 1.576233
若对需要聚合的单列使用单中括号,则输出时不会带有列标签,末尾会单独输出一行属性列
df.groupby(['key1','key2'])['data2'].mean()
Out[29]:
key1 key2
a one 0.072958
two -0.962946
b one 0.946299
two 1.576233
Name: data2, dtype: float64
若需要将多列进行聚合时,单中括号和双中括号没有区别
df.groupby(['key1','key2'])['data1','data2'].mean()
Out[31]:
data1 data2
key1 key2
a one 0.018737 0.072958
two -1.319022 -0.962946
b one 0.917741 0.946299
two -1.288477 1.576233
df.groupby(['key1','key2'])[['data1','data2']].mean()
Out[30]:
data1 data2
key1 key2
a one 0.018737 0.072958
two -1.319022 -0.962946
b one 0.917741 0.946299
two -1.288477 1.576233