이번엔 pandas에 대한 기본 툴을 배우는 시간을 가졌습니다. 우선 개략적으로 정리했던 .ipynb
에서의 필기 내용을 기록해놓겠습니다.
import pandas as pd
s = pd.Series([1,4,9,16,25])
s
딕셔너리 형태로 index name을 구성할 수 있습니다.
t= pd.Series({'one':1, 'two':2, 'three':3, 'four':4})
t
bool index 이용하여 값을 도출할 수도 있습니다.
t[t>t.median()]
s[[3,1,2]] # 여러 index호출
Series를 함수에도 담을 수 있습니다.
import numpy as np
e = np.exp(s)
'one' in t # output : True
'seven' in t # output : False
t['seven']=t.get('seven',7) # 존재하지 않는 key -> 7을 return하여 저장합니다.
t['seven'] # output : 7
s = pd.Series(np.random.normal(loc=1, scale=0.3,size=(10,)), name = 'Normalizatoin')
s
d = {"height":[1,2,3,4],"weight":[30,40,50,60]}
df = pd.DataFrame(d)
df
데이터 프레임의 data type을 확인합니다.
# dtype 확인
df.dtypes
.csv
파일을 읽어온다..read_csv()
ex1= pd.read_csv('../3w/archive/country_wise_latest.csv')
ex1.head(5) # 처음 5개 행 보여주기
ex1.tail(5) # 마지막 5개행 보여주기
ex1.describe()
df['Deaths / 100 Cases'`#해당 열을 추출
행에 bool index를 부여하여 해당 조건을 만족하는 True
인 행들의 해당 열만 뽑아올 수 있습니다.
# 신규 확진자 100명이 넘는 나라 찾기
ex1[ex1['New cases']>=100]['Country/Region']
.loc[row,col]
books = pd.read_csv('../path')
# 이때는 index name이 번호라 번호를 활용한것
# 이름이 있다면 row자리에 index name을 적으면 됨.
books.loc[9064,'Lat']
23.885942
.iloc[row_idx, col_idx]
books.iloc[9064,2]
23.885942
sum()
, mean()
, median()
을 적용하여 각 데이터 압축avo.head() # 이 데이터 프레임에 groupby를 적용한 예문이 mission3에 있습니다.
Deaths / 100 Cases
)이 가장 높은 국가는?df = pd.read_csv('../3w/archive/country_wise_latest.csv')
df[df['Deaths / 100 Cases']== df['Deaths / 100 Cases'].max()]['Country/Region']
Hint : 한 줄에 동시에 두가지 조건을 Apply하는 경우 Warning이 발생할 수 있습니다.
no_case= df[df['New cases']==0]
no_case[no_case['WHO Region']=='Europe']['Country/Region']
# 1. 'AveragePrice'열 중에서
# 2. 'region' 별 max치를 뽑아옴.
avo = pd.read_csv('../3w/archive/avocado.csv')
g = avo[['AveragePrice']].groupby(avo['region']).max()
g.head()
g