# -*- 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類似,分組的列的先后順序影響結果的層級關系,但不影響結果!