Pandas 데이터프레임 연습 2#
이 노트는 데이터프레임의 정리와 그룹화에 대한 예제이니 자유롭게 코드를 실행시키면서 연습해 보세요.
import pandas as pd
import numpy as np
먼저 연습을 위한 데이터프레임 df
를 만들어 보자.
df = pd.DataFrame( {
'I' : ["A","A","A","A","A","A","B","B","B","B","B","C","C","C","C","C","C"],
'J' : [ "math","math","math","lang","lang","lang","math","math","lang","lang","lang","math","math","math","lang","lang","lang"],
'K' : [22, 21, 31, 24, 25, 36, 31, 23, 34, 21, 29, 34, 41, 32, 26, 23, 43],
'L' : [79, 94, 64, 67, 75, 45, 65, 34, 54, 32,87, 65, 56, 65, 76, 65, 89]
})
df
I | J | K | L | |
---|---|---|---|---|
0 | A | math | 22 | 79 |
1 | A | math | 21 | 94 |
2 | A | math | 31 | 64 |
3 | A | lang | 24 | 67 |
4 | A | lang | 25 | 75 |
5 | A | lang | 36 | 45 |
6 | B | math | 31 | 65 |
7 | B | math | 23 | 34 |
8 | B | lang | 34 | 54 |
9 | B | lang | 21 | 32 |
10 | B | lang | 29 | 87 |
11 | C | math | 34 | 65 |
12 | C | math | 41 | 56 |
13 | C | math | 32 | 65 |
14 | C | lang | 26 | 76 |
15 | C | lang | 23 | 65 |
16 | C | lang | 43 | 89 |
열이름 바꾸기#
df.rename(columns= { 'I':'school', 'J': 'class', 'K' : 'age', 'L':'score'})
school | class | age | score | |
---|---|---|---|---|
0 | A | math | 22 | 79 |
1 | A | math | 21 | 94 |
2 | A | math | 31 | 64 |
3 | A | lang | 24 | 67 |
4 | A | lang | 25 | 75 |
5 | A | lang | 36 | 45 |
6 | B | math | 31 | 65 |
7 | B | math | 23 | 34 |
8 | B | lang | 34 | 54 |
9 | B | lang | 21 | 32 |
10 | B | lang | 29 | 87 |
11 | C | math | 34 | 65 |
12 | C | math | 41 | 56 |
13 | C | math | 32 | 65 |
14 | C | lang | 26 | 76 |
15 | C | lang | 23 | 65 |
16 | C | lang | 43 | 89 |
df.columns
Index(['I', 'J', 'K', 'L'], dtype='object')
df.rename(columns= { 'I':'school', 'J': 'class', 'K' : 'age', 'L':'score'}, inplace=True)
df.head(4)
school | class | age | score | |
---|---|---|---|---|
0 | A | math | 22 | 79 |
1 | A | math | 21 | 94 |
2 | A | math | 31 | 64 |
3 | A | lang | 24 | 67 |
그룹별 요약#
df1 = df.groupby(by = 'school')
함수 count()
는 그룹으로 지정된 school
의 각 그룹에 속한 행의 개수를 표시하는 함수
df1.count()
class | age | score | |
---|---|---|---|
school | |||
A | 6 | 6 | 6 |
B | 5 | 5 | 5 |
C | 6 | 6 | 6 |
df1.sum()
age | score | |
---|---|---|
school | ||
A | 159 | 424 |
B | 138 | 272 |
C | 199 | 416 |
df1.mean()
age | score | |
---|---|---|
school | ||
A | 26.500000 | 70.666667 |
B | 27.600000 | 54.400000 |
C | 33.166667 | 69.333333 |
df1.max()
class | age | score | |
---|---|---|---|
school | |||
A | math | 36 | 94 |
B | math | 34 | 87 |
C | math | 43 | 89 |
df2 = df.groupby(by = ['school', 'class'])
df2.count()
age | score | ||
---|---|---|---|
school | class | ||
A | lang | 3 | 3 |
math | 3 | 3 | |
B | lang | 3 | 3 |
math | 2 | 2 | |
C | lang | 3 | 3 |
math | 3 | 3 |
df2.sum()
age | score | ||
---|---|---|---|
school | class | ||
A | lang | 85 | 187 |
math | 74 | 237 | |
B | lang | 84 | 173 |
math | 54 | 99 | |
C | lang | 92 | 230 |
math | 107 | 186 |
df2.mean()
age | score | ||
---|---|---|---|
school | class | ||
A | lang | 28.333333 | 62.333333 |
math | 24.666667 | 79.000000 | |
B | lang | 28.000000 | 57.666667 |
math | 27.000000 | 49.500000 | |
C | lang | 30.666667 | 76.666667 |
math | 35.666667 | 62.000000 |
summary_df = df2.mean().reset_index()
summary_df
school | class | age | score | |
---|---|---|---|---|
0 | A | lang | 28.333333 | 62.333333 |
1 | A | math | 24.666667 | 79.000000 |
2 | B | lang | 28.000000 | 57.666667 |
3 | B | math | 27.000000 | 49.500000 |
4 | C | lang | 30.666667 | 76.666667 |
5 | C | math | 35.666667 | 62.000000 |
예제: 매출액 계산#
다음은 3개 전자제품 대리점 A
, B
, C
의 6개월간(1월-6월) 모니터와 핸드폰의 매출액(단위:백만원)이다.
데이터프레임 sales
의 각 열에 대한 설명은 다음과 같다.
company
: 전자제품 대리점month
: 월monitor
: 모니터 매출액 (단위:백만원)phone
: 핸드폰 매출액 (단위:백만원)
sales = pd.DataFrame( {
'company' : ["A","A","A","A","A","A","B","B","B","B","B","B","C","C","C","C","C","C"],
'month' : [1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6],
'monitor' : [22, 21, 31, 24, 25, 36, 31, 23, 34, 21, 29, 34, 41, 32, 26, 23, 43, 34],
'phone' : [79, 94, 64, 67, 75, 45, 65, 34, 54, 32,87, 65, 56, 65, 76, 65, 89, 45]
})
sales
company | month | monitor | phone | |
---|---|---|---|---|
0 | A | 1 | 22 | 79 |
1 | A | 2 | 21 | 94 |
2 | A | 3 | 31 | 64 |
3 | A | 4 | 24 | 67 |
4 | A | 5 | 25 | 75 |
5 | A | 6 | 36 | 45 |
6 | B | 1 | 31 | 65 |
7 | B | 2 | 23 | 34 |
8 | B | 3 | 34 | 54 |
9 | B | 4 | 21 | 32 |
10 | B | 5 | 29 | 87 |
11 | B | 6 | 34 | 65 |
12 | C | 1 | 41 | 56 |
13 | C | 2 | 32 | 65 |
14 | C | 3 | 26 | 76 |
15 | C | 4 | 23 | 65 |
16 | C | 5 | 43 | 89 |
17 | C | 6 | 34 | 45 |
대리점별 총매출액은?#
두 개의 메소드 groupby(by = 'company')
와 sum()
을 이용하여 대리점별로 열의 합을 구해보자.
sales.groupby(by = 'company').sum()
month | monitor | phone | |
---|---|---|---|
company | |||
A | 21 | 159 | 424 |
B | 21 | 172 | 337 |
C | 21 | 199 | 396 |
위와 같이 전체 데이터프레임에 메소드 sum()
을 적용하면 월(month
)의 값들를 더해주는 의도치 않은 결과를 얻는다.
이제 필요한 3개의 열만 선택하여 그룹을 설정하자.
sales_gr1 = sales[["company","monitor","phone"]].groupby(by = 'company')
sales_gr1
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7fa388053670>
다음으로 그룹화된 데이터프레임에 sum()
을 적용하여 구룹별로 열의 합을 구하자.
sales_gr1.sum()
monitor | phone | |
---|---|---|
company | ||
A | 159 | 424 |
B | 172 | 337 |
C | 199 | 396 |
이제 sum(axis=1)
메소드로 모니터와 핸드폰의 매출액 합을 더하면 대리점별로 총 매출액을 구할 수 있다.
sales_gr1.sum().sum(axis=1)
company
A 583
B 509
C 595
dtype: int64
하나의 표현식으로 다음과 같이 동일한 결과를 얻을 수 있다.
sales[["company","monitor","phone"]].groupby(by = 'company').sum().sum(axis=1)
company
A 583
B 509
C 595
dtype: int64
월별 총 배출액은?#
sales_gr2 = sales.groupby(by = 'month')
sales_gr2.sum().sum(axis=1)
month
1 294
2 269
3 285
4 232
5 348
6 259
dtype: int64