df = pd.read_excel('census.xlsx')
dask 설치
클러스터 설정
from dask.distributed import Client
client = Client(n_workers=4)
VBox(children=(HTML(value='<h2>LocalCluster</h2>'), HBox(children=(HTML(value='\n<div>\n <style scoped>\n …
느리게 더하는 함수
from time import sleep
def add(x, y):
sleep(1)
return x + y
Wall time: 1 s
3
%%time
x = add(1, 2)
y = add(3, 4)
add(x, y)
Wall time: 3 s
delayed
from dask.delayed import delayed
dadd = delayed(add) # dadd는 add의 delay 버전
Delayed('add-17f5f1ca-f823-4ee4-b58a-9a611ae14d93')
%%time
dadd(1, 2).compute()
Wall time: 1.02 s
3
%%time
x = dadd(1, 2)
y = dadd(3, 4)
dadd(x, y).compute()
Wall time: 2.03 s
pandas 데이터 프레임
df = pd.read_excel('census.xlsx')
dask 데이터 프레임으로 변환
import dask.dataframe as dd
df2 = dd.from_pandas(df, chunksize=10000)
평균 구하기
%%time
df2['age'].mean().compute()
여러 개의 파일을 하나의 데이터 프레임으로 처리
df.query('sex == "Male"').to_csv('male.csv')
df.query('sex == "Female"').to_csv('female.csv')
df3 = dd.read_csv('*male.csv')
df3.query('age < 18 and capital_gain > 1000').compute()
클러스터 닫기