카테고리별로 데이터 집계

Python으로 금융 데이터 가져오기와 관리

Stefan Jansen

Instructor

숫자형 데이터를 카테고리로 요약

  • 지금까지: 개별 변수 요약
  • 평균, 분위수 등 기술통계 계산
  • 데이터를 그룹으로 나눈 뒤 그룹 요약
  • 예시:
    • 거래소별 최대 기업
    • IPO 연도별 중간 시가총액
    • 섹터별 평균 시가총액
Python으로 금융 데이터 가져오기와 관리

섹터별로 데이터 그룹화

nasdaq.info()
RangeIndex: 3167 entries, 0 to 3166
Data columns (total 7 columns):
#   Column                 Non-Null Count  Dtype 
-- ---                  --------------  -----  
 0   Stock Symbol           3167 non-null   object 
 1   Company Name           3167 non-null   object 
 2   Last Sale              3165 non-null   float64
 3   Market Capitalization  3167 non-null   float64
 4   IPO Year               1386 non-null   float64
 5   Sector                 2767 non-null   object 
 6   Industry               2767 non-null   object 
dtypes: float64(3), object(4)
memory usage: 173.3+ KB
Python으로 금융 데이터 가져오기와 관리

섹터별로 데이터 그룹화

nasdaq['market_cap_m'] = nasdaq['Market Capitalization'].div(1e6)

nasdaq = nasdaq.drop('Market Capitalization', axis=1) # Drop column
nasdaq_by_sector = nasdaq.groupby('Sector') # Create groupby object
for sector, data in nasdaq_by_sector: print(sector, data.market_cap_m.mean())
Basic Industries 724.899933858
Capital Goods 1511.23737278
Consumer Durables 839.802606627
Consumer Non-Durables 3104.05120552
...
Public Utilities 2357.86531507
Technology 10883.4342135
Transportation 2869.66000673
Python으로 금융 데이터 가져오기와 관리

반복문 없이 간단히 처리

mcap_by_sector = nasdaq_by_sector.market_cap_m.mean()
mcap_by_sector
Sector
Basic Industries           724.899934
Capital Goods             1511.237373
Consumer Durables          839.802607
Consumer Non-Durables     3104.051206
Consumer Services         5582.344175
Energy                     826.607608
Finance                   1044.090205
Health Care               1758.709197
...
Python으로 금융 데이터 가져오기와 관리

카테고리 요약 시각화

title = 'NASDAQ = Avg. Market Cap by Sector'
mcap_by_sector.plot(kind='barh', title=title)
plt.xlabel('USD mn')

nasdaq_by_sector.png

Python으로 금융 데이터 가져오기와 관리

모든 숫자 열의 집계 요약

nasdaq_by_sector.mean()
                       Last Sale     IPO Year  market_cap_m
Sector                                                     
Basic Industries       21.597679  2000.766667    724.899934
Capital Goods          26.188681  2001.324675   1511.237373
Consumer Durables      24.363391  2003.222222    839.802607
Consumer Non-Durables  25.749565  2000.609756   3104.051206
Consumer Services      34.917318  2004.104575   5582.344175
Energy                 15.496834  2008.034483    826.607608
Finance                29.644242  2010.321101   1044.090205
Health Care            19.462531  2009.240409   1758.709197
Miscellaneous          46.094369  2004.333333   3445.655935
Public Utilities       18.643705  2006.040000   2357.865315
...
Python으로 금융 데이터 가져오기와 관리

Ayo berlatih!

Python으로 금융 데이터 가져오기와 관리

Preparing Video For Download...