pandas로 고급 데이터 변환

Python으로 ETL과 ELT

Jake Roach

Data Engineer

pandas로 고급 데이터 변환

변환 구성 요소가 강조된 ETL 파이프라인.

Python으로 ETL과 ELT

결측값 채우기: pandas

timestamps           volume      open     close             
1997-05-15 13:30:00  1443120000  0.121875  0.097917
1997-05-16 13:30:00   294000000  NaN       0.086458
1997-05-19 13:30:00   122136000  0.088021  NaN
# 모든 NaN을 0으로 채우기
clean_stock_data = raw_stock_data.fillna(value=0)
timestamps           volume      open     close             
1997-05-15 13:30:00  1443120000  0.121875  0.097917
1997-05-16 13:30:00   294000000  0.000000  0.086458
1997-05-19 13:30:00   122136000  0.088021  0.000000
Python으로 ETL과 ELT

결측값 채우기: pandas

timestamps           volume      open     close             
1997-05-15 13:30:00  1443120000  0.121875  0.097917
1997-05-16 13:30:00   294000000  NaN       0.086458
1997-05-19 13:30:00   122136000  0.088021  NaN
# 열별로 지정 값으로 NaN 채우기
clean_stock_data = raw_stock_data.fillna(value={"open": 0, "close": .5}, axis=1)
timestamps           volume      open     close             
1997-05-15 13:30:00  1443120000  0.121875  0.097917
1997-05-16 13:30:00   294000000  0.000000  0.086458
1997-05-19 13:30:00   122136000  0.088021  0.500000
Python으로 ETL과 ELT

결측값 채우기: pandas

timestamps           volume      open     close             
1997-05-15 13:30:00  1443120000  0.121875  0.097917
1997-05-16 13:30:00   294000000  NaN       0.086458
1997-05-19 13:30:00   122136000  0.088021  NaN
# 다른 열을 사용해 NaN 채우기
raw_stock_data["open"].fillna(raw_stock_data["close"], inplace=True)
timestamps           volume      open     close             
1997-05-15 13:30:00  1443120000  0.121875  0.097917
1997-05-16 13:30:00   294000000  0.086458  0.086458
1997-05-19 13:30:00   122136000  0.088021  NaN
Python으로 ETL과 ELT

데이터 그룹화

SELECT
    ticker,
    AVG(volume),
    AVG(open),
    AVG(close)
FROM raw_stock_data
GROUP BY ticker;

위 SQL은 pandas의 .groupby()로 재현할 수 있습니다

Python으로 ETL과 ELT

pandas로 데이터 그룹화

ticker  volume        open        close             
AAPL    1443120000    0.121875    0.097917
AAPL     297000000    0.098146    0.086458
AMZN     124186000    0.247511    0.251290
# 티커별로 그룹화하고 나머지 열의 평균을 계산
grouped_stock_data = raw_stock_data.groupby(by=["ticker"], axis=0).mean()
          volume        open        close
ticker               
AAPL      1.149287e+08    34.998377  34.986851
AMZN      1.434213e+08    30.844692  30.830233

집계에는 .min(), .max(), .sum()도 사용할 수 있습니다

Python으로 ETL과 ELT

DataFrame에 고급 변환 적용

.apply() 메서드는 더 고급 변환을 처리합니다

def classify_change(row):
    change = row["close"] - row["open"]
    if change > 0:
        return "Increase"
    else:
        return "Decrease"
# DataFrame에 변환 적용
raw_stock_data["change"] = raw_stock_data.apply(
    classify_change, 
    axis=1
)

변환 전

ticker  ...  open        close             
AAPL         0.121875    0.097917
AAPL         0.098146    0.086458
AMZN         0.247511    0.251290

$$

변환 후

ticker  ...  open        close       change  
AAPL         0.121875    0.097917    Decrease
AAPL         0.098146    0.086458    Decrease
AMZN         0.247511    0.251290    Increase
Python으로 ETL과 ELT

Ayo berlatih!

Python으로 ETL과 ELT

Preparing Video For Download...