본문 바로가기
투자

2개의 데이터 프레임을 합치는 방법

by 천장만보며 2024. 1. 6.
728x90
반응형

백테스트를 하다보니 가격 데이터와 백테스트 결과를 하나로 보고 싶어졌다. 

df=fdr.DataReader(ticker,'2020')
df.ta.bbands(length=20, std=2.0, append=True)
df.dropna(inplace=True)

기본 가격데이터는 이렇게 만들어진다. 

book_df = pd.DataFrame(book, columns=['date','stock_cnt','total_asset','stock_own','own_day','buy_price'])
book_df.set_index('date', inplace=True)

백테스트 결과는 이렇게 만들어 진다.

이때 set_index를 하지 않으면 인덱스가 없어서 merge에 문제가 생긴다.

merged_df = pd.merge(df, book_df, left_index=True, right_index=True)

두개의 데이터 프레임을 인덱스 기준으로 합치는 코드이다.

합쳐진 결과이다. 

728x90
반응형