
pandas๋ ๋ฐ์ดํฐ ์กฐ์ ๋ฐ ๋ถ์์ ์ํ ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ์ฉ์ผ๋ก ์์ฑ๋ ์ํํธ์จ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. ์ถ์ฒ : ์ํคํผ๋์
pandas๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ importํ๋ค.
import pandas as pd
s = pd.Series([1,4,9,16,25])
s
>>>0 1
1 4
2 9
3 16
4 25
dtype: int64
t = pd.Series({'one':1, 'two':2,'three':3,'four':4,'five':5})
t
>>>one 1
two 2
three 3
four 4
five 5
dtype: int64
s[1]#์ฌ๋ผ์ด์ฑ ๊ฐ๋ฅ
>>>4
t[1]
>>>2
t[1:3]
>>>two 2
three 3
dtype: int64
s[s>s.median()]#์๊ธฐ ์์ ์ median๋ณด๋ค ํฐ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ผ
>>>3 16
4 25
dtype: int64
s[[3,1,4]]#์ฌ๋ฌ๊ฐ์ง ์ธ๋ฑ์ค๋ฅผ ๋ฌถ์ด์ ๋์์ ์ถ์ถ ๊ฐ๋ฅ
>>>3 16
1 4
4 25
dtype: int64
import numpy as np
np.exp(s)#s์ ๋ํ exp๊ฐ์ด ๊ณ์ฐ๋์ด ๋์ด
>>>0 2.718282e+00
1 5.459815e+01
2 8.103084e+03
3 8.886111e+06
4 7.200490e+10
dtype: float64
s.dtype
>>>dtype('int64')
t
>>>one 1
two 2
three 3
four 4
five 5
dtype: int64
t['one']
>>>1
#Series์ ๊ฐ ์ถ๊ฐ
t['six'] = 6
t
>>>one 1
two 2
three 3
four 4
five 5
six 6
dtype: int64
'six' in t
>>>True
'seven' in t
>>>False
t.get('seven')#์ด๋ค ๊ฐ์ ๊ฐ์ ธ์ค๋ ์์ด๋ ์๋ฌ๊ฐ ๋ฐํํ์ง ์๊ณ ์์ธ์ฒ๋ฆฌ๋จ
t.get('seven',0)
>>>0
name์์ฑ์ ๊ฐ์ง๊ณ ์๋ค.s = pd.Series(np.random.randn(5),name = "random_nums")
s
>>>0 2.193934
1 -0.756458
2 -0.968082
3 0.069727
4 -0.914793
Name: random_nums, dtype: float64
#์ถ๋ ฅ๋ถ๋ถ์ ๋ณด๋ฉด Name์ด ์ถ๊ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
d = {'height':[1,2,3,4],'weight':[30,40,50,60]}
df = pd.DataFrame(d)
df

์์ ๊ฐ์ด 2์ฐจ์ ํ
์ด๋ธ ํํ๋ก ์ถ๋ ฅ๋๋ค.
## dtype ํ์ธ
#dataframe๋ ๊ฐcolumn๋ณ๋ก type์ด ๋ค๋ฅผ ์ ์๊ธฐ ๋๋ฌธ์ dtypes๋ฅผ ์ฌ์ฉ
df.dtypes
>>>height int64
weight int64
dtype: object
#๋์ผ ๊ฒฝ๋ก์ country_wise_latest.csv๊ฐ ์กด์ฌํ ๋
#covid = pd.read_csv("./country_wise_latest.csv")
#๋ด๊ฐ ์ ์ฅํ ๊ฒฝ๋ก
covid = pd.read_csv("./archive/country_wise_latest.csv")
covid

์์ ๊ฐ์ด couontry_wise_latest.csvํ์ผ์ ๋ฐ์ดํฐํ๋ ์ ํํ๋ก ๋ถ๋ฌ์จ๋ค.
head():์ฒ์ n๊ฐ์ ๋ฐ์ดํฐ ์ฐธ์กฐ# ์์์๋ถํฐ 5๊ฐ ๊ด์ฐฐํ๋ ํจ์
covid.head(5)

tail(): ๋ง์ง๋ง n๊ฐ์ ๋ฐ์ดํฐ ์ฐธ์กฐcovid.tail()

covid['Confirmed']
>>>0 36263
1 4880
2 27973
3 907
4 950
...
182 10621
183 10
184 1691
185 4552
186 2704
Name: Confirmed, Length: 187, dtype: int64
1์ฐจ์ ๋ฐ์ดํฐ์ ์ ์ฌํ๊ฒ ์ ๊ทผํ ์ ์๋ค.
covid.Active์ ํ์์ผ๋ก๋ ์ ๊ทผํ ์ ์๋๋ฐ WHO Region์ ๊ฒฝ์ฐ ์ด ๋ฐฉ์์ ์๋๋ค.
์ด๋ ๊ฒ ํด์ ์ถ์ถํ ๋ฐ์ดํฐ์ ํํ๋ Series์ด๋ค.
type(covid['Confirmed'])#DataFrame์ ๊ฐcolumn์ Series์ด๋ค.
>>>pandas.core.series.Series
๋ฐ๋ผ์
covid['Confirmed'][0]
>>>36263
์ด๋ฐ ํ์์ผ๋ก๋ ์ถ์ถ์ด ๊ฐ๋ฅํ๋ฉฐ
covid['Confirmed'][1:5]
>>>1 4880
2 27973
3 907
4 950
Name: Confirmed, dtype: int64
์ฌ๋ผ์ด์ฑ ๋ํ ๊ฐ๋ฅํ๋ค.
์กฐ๊ฑด์ ์ด์ฉํด์ ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ ์ ์๋ค.
# ์ ๊ทํ์ง์๊ฐ 100๋ช
์ด ๋๋ ๋๋ผ๋ฅผ ์ฐพ์๋ณด์
covid[covid["New cases"]>100].head(5)

New cases ๋ถ๋ถ์ ๋ณด๋ฉด ๋ค 100๋ช
์ด์์์ ๋ณผ ์ ์๋ค.
๋ค์์ WHO์ง์ญ์ด ๋๋จ์์์์ธ ๋ฐ์ดํฐ๋ง ์ถ์ถํด ๋ณด์๋ค.
# WHO ์ง์ญ(WHO Region)์ด ๋๋จ์์์์ธ ๋๋ผ ์ฐพ๊ธฐ
covid['WHO Region'].unique()
>>>array(['Eastern Mediterranean', 'Europe', 'Africa', 'Americas',
'Western Pacific', 'South-East Asia'], dtype=object)
WHO์ง์ญ์ด ๋๋จ์์์์ธ WHO Region์ด์ ์ด๋ฆ์ด ๋ฌด์์ธ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ unipue()ํจ์๋ฅผ ์ด์ฉํ์ฌ WHO Region์ ๊ฐ์ ์ถ์ถํด๋ณด์๋ค.
covid[covid['WHO Region']=='South-East Asia']

๊ทธ ํ ์กฐ๊ฑด๋ฌธ ํ์์ ํตํด WHO์ง์ญ์ด ๋๋จ์์์์ธ ๋ฐ์ดํฐ๋ง ์ถ์ถํ ์ ์์๋ค.
์ด์ ์ ์ด์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ค๋ฉด ์ด๋ฒ์ ํ์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํฉ๋๋ค.
์์์ ๋์๊ด ๋ฐ์ดํฐ๋ฅผ ์์ฑํฉ๋๋ค.
books_dict = {'Available':[True,True,True], 'Location' : [102,215,323],'Genre':['Programming','Physics','Math']}
books_df = pd.DataFrame(books_dict, index = ['๋ฒ๊ทธ๋ ๋ฌด์์ธ๊ฐ','๋๊ทผ๋๊ทผ ๋ฌผ๋ฆฌํ', '๋ฏธ๋ถํด์ค ํ์ฆ'])
books_df

books_df.loc['๋ฒ๊ทธ๋ ๋ฌด์์ธ๊ฐ']
type(books_df.loc['๋ฒ๊ทธ๋ ๋ฌด์์ธ๊ฐ'])
>>>Available True
Location 102
Genre Programming
Name: ๋ฒ๊ทธ๋ ๋ฌด์์ธ๊ฐ, dtype: object
>>>pandas.core.series.Series
์์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋ ์ธ๋ฑ์ค๋ฅผ ์ฑ ์ด๋ฆ์ผ๋ก ์ ์ฅํ๊ธฐ ๋๋ฌธ์ loc์์ ์ฑ ์ด๋ฆ์ ๋ฃ์ต๋๋ค. ์ด๋ ๊ฒ ์ถ์ถํ ๋ฐ์ดํฐ ๋๋ series์ ํํ๋ฅผ ๊ฐ์ง๋๋ค.
#์ธ๋ฑ์ค 0ํ 1์ด ๊ฐ์ ธ์ค๊ธฐ
books_df.iloc[0,1]
>>>102
iloc๋ ์ธ๋ฑ์ค๋ฅผ ์ ์๋ก๋ง ์ฝ์ด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
#์ธ๋ฑ์ค 1ํ์ ์ธ๋ฑ์ค 0-1์ด ๊ฐ์ ธ์ค๊ธฐ
books_df.iloc[1,0:2]
>>>Available True
Location 215
Name: ๋๊ทผ๋๊ทผ ๋ฌผ๋ฆฌํ, dtype: object
groupby()๋ ์ด ์ธ๋จ๊ณ๋ก ๋๋์ด์ง๋๋ค.
covid๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ groupby๋ฅผ ์คํํด๋ณด์์ต๋๋ค.
# WHO Region๋ณ ํ์ง์์
#1. covid์์ ํ์ง์ ์ column๋ง ์ถ์ถํ๋ค. : split
#2. ์ด๋ฅผ covid์ WHO Region์ ๊ธฐ์ค์ผ๋ก groupbyํ๋ค. : Apply, Combine
covid_by_region = covid['Confirmed'].groupby(by = covid["WHO Region"])#by๋ถ๋ถ์ ์๋ฆฌ์ฆํํ๋ก ์ ์ด์ฃผ๋๊ฒ ์ข๋ค.
covid_by_region.sum()
>>>WHO Region
Africa 723207
Americas 8839286
Eastern Mediterranean 1490744
Europe 3299523
South-East Asia 1835297
Western Pacific 292428
Name: Confirmed, dtype: int64
# ๋๋ฅ๋ณ ํ๊ท ๊ฐ์ผ์ ์ sum/๊ตญ๊ฐ ์
covid_by_region.mean()
>>>WHO Region
Africa 15066.812500
Americas 252551.028571
Eastern Mediterranean 67761.090909
Europe 58920.053571
South-East Asia 183529.700000
Western Pacific 18276.750000
Name: Confirmed, dtype: float64
์ฝํธ๋ฅผ ์ดํด๋ณด๋ฉด covid์์ ํ์ํ ์นผ๋ผ์ธ 'Confirmed'๋ฅผ ์ถ์ถํ์ฌ by ์ธ์๋ฅผ ๊ธฐ์ค์ผ๋ก gropby๋ฅผ ์คํํฉ๋๋ค. ๊ทธ ํ sum()ํจ์๋ฅผ ํตํด ์ฐ์ฐ์ ์ ์ฉํ๋ฉด ๋ง๋ฌด๋ฆฌ ๋ฉ๋๋ค.
covid[covid['Deaths / 100 Cases']==covid['Deaths / 100 Cases'].max()]

max()๋ฅผ ์ด์ฉํ์ฌ Deaths / 100 Cases๊ฐ ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ฐ์ง๋ ํ์ ์ถ์ถํ์๋ค.
tmp = covid.copy()
conditions =(tmp['New cases']==0)&(tmp["WHO Region"]=='Europe')
tmp = tmp[conditions]
tmp

2๊ฐ ์ด์์ ์กฐ๊ฑด์ ํ ์ค์ applyํ๋ ๊ฒ์ ๋นํจ์จ์ ์ด๋ฏ๋ก conditions๋ผ๋ ๋ณ์์ ๋ชจ๋ ์กฐ๊ฑด์ ๋ฐ๋ก ์ ์ฅํ ํ ๋ฐ์ดํฐ์ ์ ๊ทผํ์๋ค.
avocado = pd.read_csv("./avocado.csv")#๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
avocado.head()#๋ฐ์ดํฐ ์ผ๋ถ ํ์ธ
avocado.columns#์นผ๋ผ๋ช
ํ์ธ
avocado['AveragePrice'].groupby(by = avocado['region']).max()
>>>region
Albany 2.13
Atlanta 2.75
BaltimoreWashington 2.28
Boise 2.79
Boston 2.19
...
Syracuse 2.44
Tampa 3.17
TotalUS 2.09
West 2.52
WestTexNewMexico 2.93
Name: AveragePrice, dtype: float64
๋จผ์ ์ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ณ ์ ๋ฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ดํด๋ณด๊ธฐ ์ํด head()๋ฅผ ํตํด ์ผ๋ถ๋ฅผ ํ์ธํ์์ต๋๋ค.
๊ทธ ํ Region๋ณ๋ก ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํด columns()์ ์ด์ฉํด ์นผ๋ผ๋ช
์ ํ์ธํ ํ groupby().max() ๋ช
๋ น์ด๋ก ๊ฐ region๋ณ๋ก ๊ฐ์ฅ ๋น์ผ AveragePrice๋ฅผ ์ถ๋ ฅํ์ต๋๋ค.
๋~!