groupby
์ง๋จ๋ณ์ (=๋ฒ์ฃผํ ๋ฒ์, ์นดํ
๊ณ ๋ฆฌ)๋ฅผ ์ด์ฉํ ์๋ฃ ์ฒ๋ฆฌ
1. ์ง๋จ๋ณ์ ๊ธฐ์ค subset ๋ง๋ค๊ธฐ
2. ์ง๋จ๋ณ์ ๊ธฐ์ค ๊ทธ๋ฃน, ํต๊ณ๋ ๊ตฌํ๊ธฐ
3. ๊ทธ๋ฃน๊ณผ ํต๊ณ๋ ์๊ฐํ
import pandas as pd #csv file
import matplotlib.pyplot as plt #๊ทธ๋ฃน๊ณผ ํต๊ณ๋ ์๊ฐํ
dataset load & ๋ณ์ ํ์ธ
wine = pd.read_csv(r'C:\ITWILL\4_Python-2\data\winequality_both.csv')
wine.info()
๊ณต๋ฐฑ -> '_' ๊ต์ฒด
wine.columns = wine.columns.str.replace(' ','_')
wine.columns
wine.fixed_acidity
5๊ฐ ๋ณ์ ์ ํ : subset ๋ง๋ค๊ธฐ
wine_df = wine.iloc[:,[0,1,4,11,12]] #iloc : ์์น๊ธฐ๋ฐ ํ๋ ฌ ์ ํ / ํ ์ ์ฒด ์ ํ / ์ด ์์ธ
wine_df.info()
0 type 6497 non-null object
1 fixed_acidity 6497 non-null float64
2 residual_sugar 6497 non-null float64
3 alcohol 6497 non-null float64
4 quality 6497 non-null int64
์นผ๋ผ๋ช
๋ณ๊ฒฝ (์ ์ฒด๋ณ๊ฒฝ)
wine_df.columns = ['type', 'acidity', 'sugar', 'alcohol', 'quality']
์นผ๋ผ๋ช
๋ณ๊ฒฝ (์ผ๋ถ๋ถ ๋ณ๊ฒฝ) : {'old':'new'}
columns = {'fixed_acidity' : 'acidity', 'residual_sugar' : 'sugar'}
wine_df = wine_df.rename(columns = columns)
wine_df.info()
0 type 6497 non-null object
1 acidity 6497 non-null float64 -> ๋ณ๊ฒฝ
2 sugar 6497 non-null float64 -> ๋ณ๊ฒฝ
3 alcohol 6497 non-null float64
4 quality 6497 non-null int64
์ง๋จ๋ณ์ ํ์ธ : ์์ธ ์ ํ
wine_df.type.unique() #array(['red', 'white'], dtype=object)
wine_df.type.nunique() #2
wine_df['type'].value_counts()
white 4898
red 1599
์ด์ฐ๋ณ์ ํ์ธ : ์์ธ ํ์ง
wine_df.quality.unique() #[5, 6, 7, 4, 8, 3, 9]
wine_df.quality.value_counts()
6 2836 -> 6์ ํ๋ฆฌํฐ๋ฅผ ๊ฐ์ง ์์ธ์ ๋น๋์๊ฐ ์ ์ผ ๋ง๋ค
5 2138
7 1079
4 216
8 193
3 30
9 5
1. ์ง๋จ๋ณ์ ๊ธฐ์ค subset ๋ง๋ค๊ธฐ
1) 1๊ฐ ์ง๋จ ๊ธฐ์ค
red_wine = wine_df[wine['type']=='red'] #red์์ธ ์ ํ
red_wine.shape() #(1599, 5)
red_wine.head()
2) 2๊ฐ ์ด์ ์ง๋จ ๊ธฐ์ค
two_wine = wine_df[wine['type'].isin(['red', 'white'])] #type ์นผ๋ผ์ red or white ์กฐ๊ฑด ์ง์ ํด subset ๋ง๋ค๊ธฐ
two_wine.shape #(6497, 5)
two_wine.head() #red์์ธ ํ์ธ
two_wine.tail() #white์์ธ ํ์ธ
two_wine['type'].value_counts() #white 4898 / red 1599
3) ์ง๋จ๋ณ์ ๊ธฐ์ค ํน์ ๋ณ์ ์ ํ : 1์ฐจ์
์ง๋จ1 : ๋ ๋์์ธ ํ์ง
red_wine_quality = wine_df.loc[wine['type']=='red', 'quality'] #๋ช
์นญ ๊ธฐ๋ฐ ํ๋ ฌ ์ ํ
red_wine_quality.shape #(1599,) : 2์ฐจ์ -> 1์ฐจ์. quality๊ฐ ๊ฐ์ง vector๊ธธ์ด๋ง ๋ฐํ
์ง๋จ2 : ํ์ดํธ์์ธ ํ์ง
white_wine_quality = wine_df.loc[wine['type']=='white', 'quality']
white_wine_quality.shape #(4898,)
2. ์ง๋จ๋ณ์ ๊ธฐ์ค ๊ทธ๋ฃน & ํต๊ณ๋
1) ์ง๋จ๋ณ์ 1๊ฐ ์ด์ฉ ๊ทธ๋ฃนํ
ํ์) DF.groupby('์ง๋จ๋ณ์')
type_group = wine_df.groupby('type')
print(type_group) #DataFrameGroupBy object
dir(type_group) #ํธ์ถ ๊ฐ๋ฅํ ๋ฉ์๋ ํ์ธ
type_group.size() #์ง๋จ ๋ณ ๋น๋์
red 1599
white 4898
2) ์ง๋จ๋ณ ํต๊ณ๋ : group + apply
type_group.mean()
acidity sugar alcohol quality -> ํ๊ท ์ ๋ํ ํต๊ณ๋ ์ ๊ณต
type -> type์ ์ํด ๋ ์ง๋จ์ผ๋ก ๋๋จ
red 8.319637 2.538806 10.422983 5.636023
white 6.854788 6.391415 10.514267 5.877909
type_group.std() #ํ์คํธ์ฐจ
acidity sugar alcohol quality
type
red 1.741096 1.409928 1.065668 0.807569
white 0.843868 5.072058 1.230621 0.885639
ํน์ ๋ณ์ ๋์ ํต๊ณ๋
type_group['quality'].mean()
type_group['quality'].describe() #์์ฝํต๊ณ๋
2) ์ง๋จ๋ณ์ 2๊ฐ ์ด์ฉ
ํ์) DF.groupby(['์ง๋จ๋ณ์1', '์ง๋จ๋ณ์2']) : 1์ฐจ -> 2์ฐจ
wine_group = wine_df.groupby(['type','quality'])
wine_group.size()
group_size = wine_group.size()
group_size.shape #(13,) : 1์ฐจ์
1์ฐจ์ -> 2์ฐจ์
group_size_2d = group_size.unstack()
group_size_2d
quality 3 4 5 6 7 8 9
type
red 10.0 53.0 681.0 638.0 199.0 18.0 NaN -> ๊ฒฐ์ธก์น ๋ฐ์
white 20.0 163.0 1457.0 2198.0 880.0 175.0 5.0
3. ๊ทธ๋ฃน & ํต๊ณ๋ ์๊ฐํ
1) ์์ธ ์ ํ๋ณ ํ์ง : ๋น๋์ ๊ธฐ์ค
type(group_size_2d) #pandas.core.frame.DataFrame
group_size_2d.plot(kind='barh', stacked=True,
title='red vs white wind quality')
plt.show
2) ๊ทธ๋ฃน ํต๊ณ ์๊ฐํ
type_mean = type_group.mean()
type_mean[['sugar', 'alcohol']].plot(kind='bar')
plt.show()
apply
๊ทธ๋ฃน ๊ฐ์ฒด์ ์ธ๋ถํจ์ ์ ์ฉ(apply)
1. group.apply (์ธ๋ถํจ์)
group.member() vs gropy.apply(์ธ๋ถํจ์)
2. group.agg ([์ธ๋ถํจ์1, ์ธ๋ถํจ์2, ...])
dataset load
import seaborn as sns
iris = sns.load_dataset('iris')
iris.info()
ํน์ ์นผ๋ผ 1๊ฐ ๋์ ๊ทธ๋ฃน ๊ฐ์ฒด
iris_grp = iris['sepal_length'].groupby(iris['species'])
iris_grp.size()
setosa 50
versicolor 50
virginica 50
dir(iris_grp)
iris_grp.mean() #group.member()
setosa 5.006
versicolor 5.936
virginica 6.588
์ฌ์ฉ์ ์ ์ ํจ์
def avg(group) :
return group.mean()
def avg(diff) :
return group.max() - group.min()
1. group.apply(์ธ๋ถํจ์)
iris_grp.apply(sum) #๋ด์ฅํจ์
iris_grp.apply(avg) #์ฌ์ฉ์ํจ์
iris_grp.apply(max) #๋ด์ฅํจ์
iris_grp.apply(min) #๋ด์ฅํจ์
2. group.agg(['๋ด์ฅํจ์', ์ฌ์ฉ์ ์ ์ํจ์, '๋ด๋ถํจ์'])
iris_grp.agg(['sum', avg, 'max', 'min', diff])
sum avg max min
species
setosa 250.3 5.006 5.8 4.3
versicolor 296.8 5.936 7.0 4.9
virginica 329.4 6.588 7.9 4.9
pivot_table
ํผ๋ฒํ
์ด๋ธ(pivot table)
DF ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก ํ๊ณผ ์ด ๊ทธ๋ฆฌ๊ณ ๊ต์ฐจ ์
์ ํ์๋ ์นผ๋ผ์ ์ง์ ํ์ฌ ๋ง๋ค์ด์ง ํ
์ด๋ธ
ํ์) pivot_table(DF, values='๊ต์ฐจ์
์นผ๋ผ',
index = 'ํ ์นผ๋ผ', columns = '์ด ์นผ๋ผ'
,aggFunc = '๊ต์ฐจ์
์ ์ ์ฉ๋ ํจ์')
import pandas as pd
csv file load
pivot_data = pd.read_csv(r'C:\ITWILL\4_Python-2\data\pivot_data.csv')
pivot_data.info()
0 year 8 non-null int64 : ๋
๋
1 quarter 8 non-null object : ๋ถ๊ธฐ
2 size 8 non-null object : ๋งค์ถ๊ท๋ชจ
3 price 8 non-null int64 : ๋งค์ถ์ก
ํผ๋ฒํ
์ด๋ธ(pivot table)
ptable = pd.pivot_table(pivot_data, values='price',
index = ['year','quarter'], columns = 'size'
,aggfunc= 'sum')
print(ptable)
size LARGE SMALL
year quarter
2016 1Q 2000 1000
2Q 2500 1200
2017 3Q 2200 1300
4Q 2800 2300
type(ptable) #pandas.core.frame.DataFram
ptable.plot(kind='barh', stacked=True)
'๋ฐ์ดํฐ๋ถ์๊ฐ ๊ณผ์ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY48. Python Numpy (2)reshape, ๋์, ํ๋ ฌ๊ณฑ (0) | 2021.11.29 |
---|---|
DAY47. Python Numpy (1)๋ฐฐ์ด, ์์ธ, ๋ฒ์ฉํจ์ (0) | 2021.11.26 |
DAY45. Python Matplot (3)Seaborn (0) | 2021.11.23 |
DAY44. Python Matplot (2)PandasPlot (0) | 2021.11.22 |
DAY43. Python Matplot (1)Matplotlib (0) | 2021.11.19 |