본문 바로가기
SW 개발/Data 분석 (RDB, NoSQL, Dataframe)

Pandas Dataframe Groupby() 함수로 그룹별 연산하기: Split, Apply, Combine

by Kibua20 2021. 7. 25.

Pandas DataFrame에서 가장 많이 사용하는 Groupy 사용법을 설명하도록 하겠습니다. SQL 개발 경험을 가지고 있는 분이라면 GROUPBY를 높은 빈도로 사용했을 것입니다. 

 

Groupby 동작 방식은 Pandas 공식 사이트(링크)에 자세히 설명되어 있습니다. Groupby()는 ① 전체 데이터를 그룹별로 분할(split)하고, ② mean(), sum(), count()와 같은 Aggregate function을 사용하여 연산(apply)하고, 연산 결과를  ③ 다시 합치는(combine) 과정을 거치게 됩니다. 

  • Splitting the data into groups based on some criteria.
  • Applying a function to each group independently.
  • Combining the results into a data structure.

 

Apply 단계는 Aggregation, Transformation, Filteration 3가지 연산이 존재합니다.  Aggregation은 Split 한 그룹의 평균, Variation, Size 등과 같은 통계적인 값을 계산합니다. Transformation은 한 그룹의 계산 결과를 index object처럼 추가합니다. Filteration은 특정 조건을 만족하는 Boolean column을 만들어 특정 조건의 그룹을 새로 만듭니다.

  • Aggregation: compute a summary statistic (or statistics) for each group. Some examples:
    • Compute group sums or means
    • Compute group sizes / counts.
  • Transformation: perform some group-specific computations and return a like-indexed object. Some examples:
    • Standardize data (zscore) within a group.
    • Filling NAs within groups with a value derived from each group.
  • Filtration: discard some groups, according to a group-wise computation that evaluates True or False. Some examples:
    • Discard data that belongs to groups with only a few members.
    • Filter out data based on the group sum or mean.

아래 그림은 Groupy()의 동작 방식인 Split-Apply-Combine을 설명하는 예제입니다.  Column은 Grade(학년), Name(이름), Test1, Test2, Test3, Test4, Final(점수),  Level (평가 결과)이고, 각 학생별로 실제 값들이 Row로 저장되어 있는 데이터입니다.  Groupby('Grade') 함수의 동작은 각 Grade별도 Dataframe을 split 하고, Aggregate function에 의해서 각각 나눠진 dataframe에서 연산을 apply 하고, 그 결과를 다시 합쳐(Combine)해서 새로운 Dataframe을 만듭니다.

Dataframe Groupby() 함수 동작

 

import pandas as pd

# csv file에서 읽기

# df = pd.read_csv('./Grade_sample.csv')

df = pd.read_csv('https://raw.githubusercontent.com/kibua20/devDocs/master/pandas_example/Grade_Sample.csv')

 

# groupby()

df_groupby = df.groupby(['Grade']).mean()

print (df_groupby) 

Dataframe groupby() 예제

 

 

Split, Apply, Combine의 Groupby의  사용 예제 

간단한 예제로 Groupyby의 연산 방식을 확인하도록 하겠습니다.   A, B, C, D column이 있고, A와 B column은 string 값으로 구성되어 있고, C와 D는 임의의 숫자로 구성되어 있습니다. Groupby('A')는 A값을 기준으로 'bar'와 'fool' 2개의 group으로 분리하고 각 그룹에 대해서 sum() 연산을 적용하면 새로운 dataframe을 생성합니다. 이와 비슷하게   Groupby(['A', 'B'])는 'bar' 그룹에서 B column의 각 그룹으로 분리하고 각각의 분리된 dataframe에서 sum() 연산을 수행합니다. 연산 결과는 아래 그림과 같습니다.  

 

df = pd.DataFrame(

{

    "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],

    "B": ["one", "one", "two", "three", "two", "two", "one", "three"],

    "C": np.random.randn(8),

    "D": np.random.randn(8),

}

)

 

grouped = df.groupby(["A"])

for name, group in grouped:

   print(name)

   print(group)

df.groupby(["A"]).sum()

df.groupby(["A", "B"]).sum()

 

Groupby 의 Split, Apply, Combine 예제

 



grouped.size() 함수는 각 그룹의 크기를 return 하고, grouped.described() 함수는 각각의 통계치를 리턴합니다.

groupby의 size()함수의 describe() 함수

 

df.groupby(["A", "B"], as_index=False).sum() 은 리턴하는 Dataframe의 Index column을 생성하지 않고, column을 그대로 유지한 상태에서  Aggregate 함수의 결과를 계산합니다.

 

as_index=False 결과

다수의 aggregate 함수도 사용 가능합니다. 

 

grouped = df.groupby("A")

grouped.agg([np.sum, np.mean, np.std])

grouped["C"].agg([np.sum, np.mean, np.std])

다수의 aggregagte 함수

 

Dataframe에서 기본적으로 제공하는 Aggregate 함수는 아래와 같습니다. 

size(),  count(), sum(), mean(), median(), min(), max(), mode(), std(), var() 등의 함수입니다.  사용자 정의 aggregate 함수도 정의 가능합니다.

Built-in aggregate 함수

 aggregate function에 각각의 column과 함수에 해당하는 dict를 전달하면 각각의 column에 대해서 다른 aggregate 함수로 연산합니다.

다수의 aggregate 함수 - dict로 전달

filter() 함수를 통해서 특정 조건에 맞는 group의 dataframe을 만들 수 있습니다. 아래 예제는 split 한 'B' Column의 개수가 2개 이상인 group에 대해서만 split한 결과입니다.

 

dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")})

grouped = dff.groupby("B")

dff.groupby("B").filter(lambda x: len(x) > 2)

Group by filter()  함수

 

apply() 함수는 Aggregate, Transform, Filteration 등의 제약 제약 없이 일반적인 함수를 정의하여 연산할 수 있습니다. 

grouped = df.groupby("A")

grouped["C"].apply(lambda x: x.describe())

Groupby의 apply() 함수

 

관련 글:

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Panda Dataframe 날짜 기준으로 데이터 조회 및 처리하기

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Pandas Dataframe 여러 열과 행에 apply() 함수 적용 (Sample code 포함)

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Jupyter Notebook의 업그레이드: Jupyter Lab 설치 및 extension 사용법

[개발환경/우분투] - 대용량 파일을 작은 크기로 분할하는 방법: split

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Dataframe(또는 Numpy)를 JSON 출력 시 에러: TypeError: Object of type int64 is not JSON serializable

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Python Pandas로 Excel과 CSV 파일 입출력 방법

[SW 개발/Python] - Python 정규식(Regular Expression) re 모듈 사용법 및 예제

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Python KoNLPy와 WordCloud를 활용하여 WordCloud 생성하기 (Sample code 포함)

[SW 개발/Python] - Python: JSON 개념과 json 모듈 사용법

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Apple App Store 사용자 댓글(리뷰) 데이터 수집하기 (Sample code 포함)

[SW 개발/Data 분석 (RDB, NoSQL, Dataframe)] - Panda Dataframe 날짜 기준으로 데이터 조회 및 처리하기

[SW 개발/Android] - Python으로 개발된 Android Apk Decompile Tool: androguard

[개발환경/Web Server] - Python: Web Framework Flask 사용하기




댓글