'urllib2.urlopen()'에 해당되는 글 1건

  1. 2018.09.02 [Python pandas] 여러개의 함수를 적용하여 GroupBy 집계하기 : grouped.agg() 2

지난번 포스팅에서는 Python pandas의 GroupBy 집계 메소드와 함수에 대해서 알아보았습니다. 


이번 포스팅에서는 Python pandas의 GroupBy 집계를 할 때 grouped.agg() 를 사용하여 다수의 함수를 적용하는 몇 가지 방법을 소개하고자 합니다. 


(1) 함수 리스트(List)를 사용하여 다수의 GroupBy 집계 함수를 동일한 칼럼에 적용하기

(2) 칼럼과 함수를 매핑한 Dict를 사용하여 칼럼별로 특정 GroupBy 집계 함수를 적용하기

(3) (이름, 함수)의 튜플 (Tuples of (name, function))을 사용하여 GroupBy 집계 함수에 이름 부여하기



[ Python pandas: GroupBy with multiple functions using lists, Dicts, tuples ]



예제로 사용할 데이터는 UCI Machine Learning Repository에 있는 Abalone data set 입니다. 전복의 둘레, 두께, 높이, 전체 무게, 껍질 무게 등 4,177개의 전복을 측정해 놓은 데이터셋입니다. 



[ UCI Machine Learning Repository ]

Name Data Type Meas. Description ---- --------- ----- ----------- Sex nominal M, F, and I (infant) Length continuous mm Longest shell measurement Diameter continuous mm perpendicular to length Height continuous mm with meat in shell Whole weight continuous grams whole abalone Shucked weight continuous grams weight of meat Viscera weight continuous grams gut weight (after bleeding) Shell weight continuous grams after being dried Rings integer +1.5 gives the age in years



UCI machine learning repository 웹사이트로부터 Abalone 데이터셋을 csv파일을 다운로드 받아서 pandas DataFrame로 불러오도록 하겠습니다. 



# Importing common libraries

import numpy as np

import pandas as pd

 

# Import Abalone data set from UCI machine learning repository directly

import csv

import urllib2

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data'

downloaded_data  = urllib2.urlopen(url)

abalone = pd.read_csv(downloaded_data, 

                      names = ['sex', 'length', 'diameter', 'height', 

                               'whole_weight', 'shucked_weight', 'viscera_weight', 

                               'shell_weight', 'rings'], 

                      header = None)


abalone.head()

sexlengthdiameterheightwhole_weightshucked_weightviscera_weightshell_weightrings
0M0.4550.3650.0950.51400.22450.10100.15015
1M0.3500.2650.0900.22550.09950.04850.0707
2F0.5300.4200.1350.67700.25650.14150.2109
3M0.4400.3650.1250.51600.21550.11400.15510
4I0.3300.2550.0800.20500.08950.03950.0557




예제에서 GroupBy 집계 시 그룹을 나누는 기준으로 사용할 용도로 'length' 변수에 대해 중앙값을 기준으로 큰지, 작은지 여부에 따라 'length_cat' 라는 범주형 변수를 하나 더 만들어보겠습니다. 



abalone['length_cat'] = np.where(abalone.length > np.median(abalone.length), 

                                 'length_long', # True

                                 'length_short') # False


abalone[['length', 'length_cat']][:10]

lengthlength_cat
00.455length_short
10.350length_short
20.530length_short
30.440length_short
40.330length_short
50.425length_short
60.530length_short
70.545length_short
80.475length_short
90.550length_long

 





 (1) 함수 리스트(List)를 사용하여 다수의 GroupBy 집계 함수를 동일한 칼럼에 적용하기


'sex' ('F', 'I', 'M' 계급), 'length_cat' ('length_short', 'length_long' 계급) 의 두 개의 범주형 변수를 사용하여 GroupBy 집계 시 그룹을 나누는 기준으로 사용하겠으며, 'whole_weight' 연속형 변수에 대해 GroupBy 집계 함수를 적용하여 그룹 집계를 해보겠습니다.  



grouped_ww = abalone.groupby(['sex', 'length_cat'])['whole_weight']

grouped_ww

<pandas.core.groupby.SeriesGroupBy object at 0x10a7e0290> 





먼저, 복습을 하는 차원에서 지난번 포스팅에서 소개했던 '단일 함수'를 사용하여 GroupBy 집계하는 두가지 방법, 즉 (1) GroupBy method를 사용하거나 아니면 (2) grouped.agg(함수)를 사용하는 방법을 소개하면 아래와 같습니다. 하나의 집계함수를 적용하면 반환되는 결과는 Series 가 됩니다. 


(방법1) GroupBy methods

(방법2) grouped.agg(function)

 

grouped_ww.mean() # Series

sex  length_cat  
F    length_long     1.261330
     length_short    0.589702
I    length_long     0.923215
     length_short    0.351234
M    length_long     1.255182
     length_short    0.538157
Name: whole_weight, dtype: float64

 

grouped_ww.agg('mean') # Series

sex  length_cat  
F    length_long     1.261330
     length_short    0.589702
I    length_long     0.923215
     length_short    0.351234
M    length_long     1.255182
     length_short    0.538157
Name: whole_weight, dtype: float64




이제부터 '여러개의 함수'를 적용하여 GroupBy 집계하는 방법을 소개하겠습니다. 먼저, GroupBy 집계하려는 함수들의 문자열 리스트(list)로 grouped.agg() 에 적용하는 방법입니다.  이처럼 여러개의 집계함수를 적용하면 반환되는 결과는 DataFrame이 됩니다. 



grouped_ww.agg(['size', 'mean', 'std', 'min', 'max']) # DataFrame

sizemeanstdminmax
sexlength_cat
Flength_long8891.2613300.3296560.64052.6570
length_short4180.5897020.2024000.08001.3580
Ilength_long1880.9232150.2183340.55852.0495
length_short11540.3512340.2042370.00201.0835
Mlength_long9661.2551820.3546820.59902.8255
length_short5620.5381570.2464980.01551.2825




function_list = ['size', 'mean', 'std', 'min', 'max']

grouped_ww.agg(function_list)

sizemeanstdminmax
sexlength_cat
Flength_long8891.2613300.3296560.64052.6570
length_short4180.5897020.2024000.08001.3580
Ilength_long1880.9232150.2183340.55852.0495
length_short11540.3512340.2042370.00201.0835
Mlength_long9661.2551820.3546820.59902.8255
length_short5620.5381570.2464980.01551.2825

 




물론, "다수의 칼럼"에 대해서 여러개의 함수를 적용하는 것도 가능합니다. 아래의 예에서는 'whole_weight', 'shell_weight'의 두 개의 칼럼에 대해서 GroupBy 집계 함수 리스트(list)를 적용하여 집계하여 보았습니다. 



grouped = abalone.groupby(['sex', 'length_cat'])

function_list = ['size', 'mean', 'std']

groupby_result = grouped['whole_weight', 'shell_weight'].agg(function_list)

groupby_result

whole_weightshell_weight
sizemeanstdsizemeanstd
sexlength_cat
Flength_long8891.2613300.3296568890.3600130.104014
length_short4180.5897020.2024004180.1786500.063085
Ilength_long1880.9232150.2183341880.2732470.064607
length_short11540.3512340.20423711540.1045490.061003
Mlength_long9661.2551820.3546829660.3516830.102636
length_short5620.5381570.2464985620.1621410.075629




GroupBy 집계 결과가 pandas DataFrame으로 반환된다고 하였으므로, DataFrame에서 사용하는 Indexing 기법을 그대로 사용할 수 있습니다. 예를 들어, 칼럼을 기준으로 집계 결과 데이터프레임인 groupby_result 로 부터 'shell_weight' 변수에 대한 결과만 Indexing 해보겠습니다. 



groupby_result['shell_weight']

sizemeanstd
sexlength_cat
Flength_long8890.3600130.104014
length_short4180.1786500.063085
Ilength_long1880.2732470.064607
length_short11540.1045490.061003
Mlength_long9660.3516830.102636
length_short5620.1621410.075629

 


groupby_result['shell_weight'][['size', 'mean']]

sizemean
sexlength_cat
Flength_long8890.360013
length_short4180.178650
Ilength_long1880.273247
length_short11540.104549
Mlength_long9660.351683
length_short5620.162141





GroupBy 집계 결과 데이터프레임으로부터 row를 기준으로 Indexing을 할 수도 있습니다. DataFrame에서 row 기준으로 indexing 할 때 DataFrame.loc[] 를 사용하는 것과 동일합니다. 



groupby_result.loc['M']

whole_weightshell_weight
sizemeanstdsizemeanstd
length_cat
length_long9661.2551820.3546829660.3516830.102636
length_short5620.5381570.2464985620.1621410.075629

 


groupby_result.loc['M', 'shell_weight']

sizemeanstd
length_cat
length_long9660.3516830.102636
length_short5620.1621410.075629






 (2) 칼럼과 함수를 매핑한 Dict를 사용하여 칼럼별로 특정 GroupBy 집계 함수를 적용하기 


먼저, 범위(range)와 IQR(Inter-Quartile Range)를 구하는 사용자 정의 함수를 정의한 후에 grouped.agg() 에 적용해보겠습니다.  



def range_func(x):

    max_val = np.max(x)

    min_val = np.min(x)

    range_val = max_val - min_val

    return range_val


def iqr_func(x):

    q3, q1 = np.percentile(x, [75, 25])

    iqr = q3 - q1

    return iqr

 




이제 Dicts를 사용하여 'whole_weight' 칼럼에는 size(), mean(), std() 메소드를 매핑하여 GroupBy 집계에 적용하고, 'shell_weight' 칼럼에는 range_func, iqr_func 사용자 정의 함수를 매핑하여 GroupBy 집계에 적용해보겠습니다. 


size(), mean(), std() 등의 메소드는 문자열(string)로 grouped.agg() 안에 넣어주어야 해서 작은따옴표('method_name')로 감싸주었으며, 사용자 정의 함수(UDF)는 작은따옴표 없이 그냥 써주면 됩니다. 



grouped.agg({'whole_weight': ['size', 'mean', 'std'], # put method's name as a string

            'shell_weight': [range_func, iqr_func]}) # UDF name

whole_weightshell_weight
sizemeanstdrange_funciqr_func
sexlength_cat
Flength_long8891.2613300.3296560.8500.127000
length_short4180.5897020.2024000.3780.080500
Ilength_long1880.9232150.2183340.4850.067875
length_short11540.3512340.2042370.3490.092750
Mlength_long9661.2551820.3546820.7760.124000
length_short5620.5381570.2464980.3750.102750

 





 (3) (이름, 함수)의 튜플 (Tuples of (name, function))을 사용하여 GroupBy 집계 함수에 이름 부여하기


위의 (2)번에서 Dicts를 사용하여 shell_weight 변수에 대해 range_func, iqr_func 사용자 정의 함수를 적용하여 GroupBy 집계를 하였는데요, 집계 결과로 반환된 데이터프레임의 변수 이름이 그대로 'range_func', 'iqr_func' 여서 왠지 좀 마음에 들지 않군요.  이럴 때 (이름, 함수) 의 튜플 (Tuples of (name, function))을 사용하여 함수에 특정 이름을 부여할 수 있습니다. 


아래 예제에서는 알아보기에 쉽도록 'range_func'는 'Range'라는 이름으로, 'iqr_func'는 'Inter-Quartile_Range'라는 이름을 부여하여 변경을 해보겠습니다. 



# (name, function) tuples

grouped.agg({'whole_weight': ['size', 'mean', 'std'], 

            'shell_weight': [('Range', range_func),  # (name, function) tuple

                                    ('Inter-Quartile_Range', iqr_func)]}) # (name, function) tuple

whole_weightshell_weight
sizemeanstdRange

Inter-Quartile_Range

sexlength_cat
Flength_long8891.2613300.3296560.8500.127000
length_short4180.5897020.2024000.3780.080500
Ilength_long1880.9232150.2183340.4850.067875
length_short11540.3512340.2042370.3490.092750
Mlength_long9661.2551820.3546820.7760.124000
length_short5620.5381570.2464980.3750.102750



많은 도움이 되었기를 바랍니다. 

728x90
반응형
Posted by Rfriend
,