데이터의 가능성/pandas

05_Time_Series_Investor_Flow_of_Funds_US

gamnyam 2024. 6. 24. 13:51

1. 필요한 라이브러리들을 import 하세요.

import pandas as pd

 

2. 다음과 같은 DataSet을 import 하세요.

url = "https://raw.githubusercontent.com/myoh0623/dataset/main/weekly.csv"

 

3. df라는 변수에 DataFrame을 할당하세요.

df = pd.read_csv(url)

 

4. Date column을 index로 할당하세요.

df.set_index('Date', inplace=True, drop=True)

 

5. index의 data type은 무엇인가요?

df.index.dtype

 dtype('O')

 

6. index를 DatetimeIndex type으로 변경하세요.

df.index = pd.to_datetime(df.index)

 

7. frequency 를 월 단위로 변경하고(월말 기준), 각 column의 월단위로 합산값을 구하세요.

monthly = df.resample("ME").sum()

 

8. 위의 결과 중 NaN(or 0)으로 채워진 값이 존재하는 row를 삭제 하세요.

monthly = monthly[monthly != 0].dropna(axis=0)

 

0이 아닌 것들 찾고 그 중 결측치(nan) 있는 행 제거

(axis=0) : 행

 

9. 위의 결과를 이용해 연별 데이터로 변경하세요.(매년 1월 1일 기준)

monthly.resample("YS").sum()

 

cf)

monthly.resample("YS-JAN").sum()
monthly.resample("YS-DEC").sum()

 

Time series / date functionality — pandas 2.2.2 documentation (pydata.org)

 

Time series / date functionality — pandas 2.2.2 documentation

Time series / date functionality pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python

pandas.pydata.org