#-*-Coding:utf-8-*-
"""
Created on Sat June 30 10:09:47 2018
Test group GroupBy
@author: Zhen
"""
From pandas import DataFrame
"""
data = [
[1,2,2,1]
[2,2,2,2]
[1,3,3,2]
[2,2,2,4]
]
"""
# Create test data, convert dictionary into data frame
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 (' ===================================== ')
Results:
Analysis:
according to the analysis of the results: groupby in Python is similar to SQL, and the order of grouped columns affects the hierarchical relationship of results, but does not affect the results!
The GroupBy of Python