# python DataFrame数据分组统计groupby()函数，值得推荐

• 4. 通过 字典 和 Series 对象进行分组统计

• 4.1通过一个字典
• 4.2通过一个Series

1. groupby基本用法

=====================================================================================

1.1 一级分类_分组求和

import pandas as pd

data = [[‘a’, ‘A’, 109], [‘b’, ‘B’, 112], [‘c’, ‘A’, 125], [‘d’, ‘C’, 120],

[‘e’, ‘C’, 126], [‘f’, ‘B’, 133], [‘g’, ‘A’, 124], [‘h’, ‘B’, 134],

[‘i’, ‘C’, 117], [‘j’, ‘C’, 128]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

columns = [‘name’, ‘class’, ‘num’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("=================================================")

df1 = df.groupby(‘class’).sum() # 分组统计求和

print(df1)

1.2 二级分类_分组求和

import pandas as pd

data = [[‘a’, ‘A’, ‘1等’, 109], [‘b’, ‘B’, ‘1等’, 112], [‘c’, ‘A’, ‘1等’, 125], [‘d’, ‘B’, ‘2等’, 120],

[‘e’, ‘B’, ‘1等’, 126], [‘f’, ‘B’, ‘2等’, 133], [‘g’, ‘A’, ‘2等’, 124], [‘h’, ‘B’, ‘1等’, 134],

[‘i’, ‘A’, ‘2等’, 117], [‘j’, ‘A’, ‘2等’, 128], [‘h’, ‘A’, ‘1等’, 130], [‘i’, ‘B’, ‘2等’, 122]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

columns = [‘name’, ‘class_1’, ‘class_2’, ‘num’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("=================================================")

df1 = df.groupby([‘class_1’, ‘class_2’]).sum() # 分组统计求和

print(df1)

1.3 对DataFrameGroupBy对象列名索引（对指定列统计计算）

import pandas as pd

data = [[‘a’, ‘A’, ‘1等’, 109], [‘b’, ‘B’, ‘1等’, 112], [‘c’, ‘A’, ‘1等’, 125], [‘d’, ‘B’, ‘2等’, 120],

[‘e’, ‘B’, ‘1等’, 126], [‘f’, ‘B’, ‘2等’, 133], [‘g’, ‘A’, ‘2等’, 124], [‘h’, ‘B’, ‘1等’, 134],

[‘i’, ‘A’, ‘2等’, 117], [‘j’, ‘A’, ‘2等’, 128], [‘h’, ‘A’, ‘1等’, 130], [‘i’, ‘B’, ‘2等’, 122]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

columns = [‘name’, ‘class_1’, ‘class_2’, ‘num’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("=================================================")

df1 = df.groupby(‘class_1’)[‘num’].sum()

print(df1)

2. 对分组数据进行迭代

===================================================================================

2.1 对一级分类的DataFrameGroupBy对象进行遍历

for name, group in DataFrameGroupBy_object

import pandas as pd

data = [[‘a’, ‘A’, ‘1等’, 109], [‘b’, ‘C’, ‘1等’, 112], [‘c’, ‘A’, ‘1等’, 125], [‘d’, ‘B’, ‘2等’, 120],

[‘e’, ‘B’, ‘1等’, 126], [‘f’, ‘B’, ‘2等’, 133], [‘g’, ‘C’, ‘2等’, 124], [‘h’, ‘A’, ‘1等’, 134],

[‘i’, ‘C’, ‘2等’, 117], [‘j’, ‘A’, ‘2等’, 128], [‘h’, ‘B’, ‘1等’, 130], [‘i’, ‘C’, ‘2等’, 122]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

columns = [‘name’, ‘class_1’, ‘class_2’, ‘num’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("===============================")

# 获取目标数据。

df1 = df[[‘name’, ‘class_1’, ‘num’]]

for name, group in df1.groupby(‘class_1’):

print(name)

print("=============================")

print(group)

print("==================================================")

2.2 对二级分类的DataFrameGroupBy对象进行遍历

for (key1, key2), group in df.groupby([‘class_1’, ‘class_2’]) 为例

import pandas as pd

data = [[‘a’, ‘A’, ‘1等’, 109], [‘b’, ‘C’, ‘1等’, 112], [‘c’, ‘A’, ‘1等’, 125], [‘d’, ‘B’, ‘2等’, 120],

[‘e’, ‘B’, ‘1等’, 126], [‘f’, ‘B’, ‘2等’, 133], [‘g’, ‘C’, ‘2等’, 124], [‘h’, ‘A’, ‘1等’, 134],

[‘i’, ‘C’, ‘2等’, 117], [‘j’, ‘A’, ‘2等’, 128], [‘h’, ‘B’, ‘1等’, 130], [‘i’, ‘C’, ‘2等’, 122]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

columns = [‘name’, ‘class_1’, ‘class_2’, ‘num’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("===============================")

for (key1, key2), group in df.groupby([‘class_1’, ‘class_2’]):

print(key1, key2)

print("=============================")

print(group)

print("==================================================")

(部分)

3. agg()函数

=================================================================================

3.1一般写法_对目标数据使用同一聚合函数

df1.groupby([‘class_1’, ‘class_2’]).agg([‘mean’, ‘sum’])

import pandas as pd

data = [[‘a’, ‘A’, ‘1等’, 109, 144], [‘b’, ‘C’, ‘1等’, 112, 132], [‘c’, ‘A’, ‘1等’, 125, 137], [‘d’, ‘B’, ‘2等’, 120, 121],

[‘e’, ‘B’, ‘1等’, 126, 136], [‘f’, ‘B’, ‘2等’, 133, 127], [‘g’, ‘C’, ‘2等’, 124, 126], [‘h’, ‘A’, ‘1等’, 134, 125],

[‘i’, ‘C’, ‘2等’, 117, 125], [‘j’, ‘A’, ‘2等’, 128, 133], [‘h’, ‘B’, ‘1等’, 130, 122], [‘i’, ‘C’, ‘2等’, 122, 111]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

columns = [‘name’, ‘class_1’, ‘class_2’, ‘num1’, ‘num2’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("===============================")

df1 = df[[‘class_1’, ‘class_2’, ‘num1’, ‘num2’]]

print(df1.groupby([‘class_1’, ‘class_2’]).agg([‘mean’, ‘sum’]))

3.2 对不同列使用不同聚合函数

import pandas as pd

data = [[‘a’, ‘A’, ‘1等’, 109, 144], [‘b’, ‘C’, ‘1等’, 112, 132], [‘c’, ‘A’, ‘1等’, 125, 137], [‘d’, ‘B’, ‘2等’, 120, 121],

[‘e’, ‘B’, ‘1等’, 126, 136], [‘f’, ‘B’, ‘2等’, 133, 127], [‘g’, ‘C’, ‘2等’, 124, 126], [‘h’, ‘A’, ‘1等’, 134, 125],

[‘i’, ‘C’, ‘2等’, 117, 125], [‘j’, ‘A’, ‘2等’, 128, 133], [‘h’, ‘B’, ‘1等’, 130, 122], [‘i’, ‘C’, ‘2等’, 122, 111]]

index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

columns = [‘name’, ‘class_1’, ‘class_2’, ‘num1’, ‘num2’]

df = pd.DataFrame(data=data, index=index, columns=columns)

print(df)

print("===============================")

df1 = df[[‘class_1’, ‘num1’, ‘num2’]]

print(df1.groupby(‘class_1’).agg({‘num1’: [‘mean’, ‘sum’], ‘num2’: [‘sum’]}))

![在这里插入图片描述](https://img-b

log.csdnimg.cn/690bbc0b701a4b4f9760a05c7d79a7f7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5L6v5bCP5ZW-,size_20,color_FFFFFF,t_70,g_se,x_16)

### 一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理，形成各个领域的知识点汇总，它的用处就在于，你可以按照上面的知识点去找对应的学习资源，保证自己学得较为全面。

e0d10d0a6a2.png)

THE END