# -*- coding: utf-8 -*-
“””
Created on Sat Jun 30 10:09:47 2018
测试分组groupby
@author: zhen
“””
from pandas import DataFrame
“””
data = [
[1,2,2,1]
[2,2,2,2]
[1,3,3,2]
[2,2,2,4]
]
“””
# 创建测试数据,将字典转换成为数据框
df = DataFrame({‘a’:[1,2,2,1],’b’:[2,2,2,2],’c’:[1,3,3,2],’d’:[2,2,1,4]})
show2 = df.groupby([‘a’,’b’,’c’])[‘c’].agg([‘max’,’min’,’mean’])
show3 = df.groupby([‘b’,’a’,’c’])[‘c’].agg([‘max’,’min’,’mean’])
print(‘=====================================’)
print(df)
print(‘=====================================’)
print(show2)
print(‘=====================================’)
print(show3)
print(‘=====================================’)
结果:
分析:
根据结果分析可知:Python中的groupby与sql类似,分组的列的先后顺序影响结果的层级关系,但不影响结果!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13003.html