# 学习Pandas，第 6 课

``````# 导入库
import pandas as pd
import sys
``````
``````print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
``````
``````Python version 3.6.1 | packaged by conda-forge | (default, Mar 23 2017, 21:57:00)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Pandas version 0.19.2
``````
``````# 我们的小数聚集
d = {'one':[1,1,1,1,1],
'two':[2,2,2,2,2],
'letter':['a','a','b','b','c']}

# 创建一个 dataframe
df = pd.DataFrame(d)
df
``````
letter one two
0 a 1 2
1 a 1 2
2 b 1 2
3 b 1 2
4 c 1 2
``````# 创建一个 groupby 对象
one = df.groupby('letter')

# 在分组上应用 sum() 函数
one.sum()
``````
one two
letter
a 2 4
b 2 4
c 1 2
``````letterone = df.groupby(['letter','one']).sum()
letterone
``````
two
letter one
a 1 4
b 1 4
c 1 2
``````letterone.index
``````
``````MultiIndex(levels=[['a', 'b', 'c'], [1]],
labels=[[0, 1, 2], [0, 0, 0]],
names=['letter', 'one'])
``````

``````letterone = df.groupby(['letter','one'], as_index=False).sum()
letterone
``````
letter one two
0 a 1 4
1 b 1 4
2 c 1 2
``````letterone.index
``````
``````Int64Index([0, 1, 2], dtype='int64')
``````

This tutorial was created by HEDARO

These tutorials are also available through an email course, please visit http://www.hedaro.com/pandas-tutorial to sign up today.

``````

``````