데이터 집계의 더 많은 방법

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

Stefan Jansen

Instructor

여러 가지 집계 방법

  • 지난 세그먼트: 하나의 변수로 그룹화하고 {{1}} 집계
  • 데이터를 더 정교하게 요약하는 방법:
    • 두 개 이상 변수로 그룹화
    • 여러 집계 적용
  • 예시
    • 섹터·IPO 연도별 시가총액 중앙값
    • 연도별 주가 평균·표준편차
Python으로 금융 데이터 가져오기와 관리

카테고리별 여러 집계

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

by_sector.market_cap_m.agg(['size', 'mean']).sort_values('size')
Sector                 size          mean         
Transportation           52   2869.660007
Energy                   66    826.607608
Public Utilities         66   2357.865315
Basic Industries         78    724.899934
...
Consumer Services       348   5582.344175
Technology              433  10883.434214
Finance                 627   1044.090205
Health Care             645   1758.709197
Python으로 금융 데이터 가져오기와 관리

여러 집계와 새 레이블

by_sector.market_cap_m.agg(['size', 'mean'])
      .rename(columns={'size': '#Obs', 'mean': 'Average'})
Sector                #Obs       Average           
Basic Industries        78    724.899934
Capital Goods          172   1511.237373
Consumer Durables       88    839.802607
Consumer Non-Durables  103   3104.051206
Consumer Services      348   5582.344175
...
Health Care            645   1758.709197
Miscellaneous           89   3445.655935
Public Utilities        66   2357.865315
Technology             433  10883.434214
Transportation          52   2869.660007
Python으로 금융 데이터 가져오기와 관리

열별로 다른 통계

by_sector.agg({'market_cap_m': 'size', 'IPO Year':'median'})
Sector                 market_cap_m  IPO Year    
Basic Industries                 78    1972.0
Capital Goods                   172    1972.0
Consumer Durables                88    1983.0
Consumer Non-Durables           103    1972.0
Consumer Services               348    1981.0
...
Health Care                     645    1981.0
Miscellaneous                    89    1987.0
Public Utilities                 66    1981.0
Technology                      433    1972.0
Transportation                   52    1986.0
Python으로 금융 데이터 가져오기와 관리

두 카테고리로 집계

by_sector_year = nasdaq.groupby(['Sector', 'IPO Year'])

by_sector_year.market_cap_m.mean()
Sector            IPO Year
Basic Industries  1972.0      877.240005
                  1973.0     1445.697371
                  1986.0     1396.817381
                  ...
Transportation    1986.0     1176.179710
                  1991.0     6646.778622
                  1992.0       56.074572
                  ...
                  2009.0      552.445919
                  2011.0     3711.638317
                  2013.0      125.740421
Python으로 금융 데이터 가져오기와 관리

멀티인덱스에서 선택하기

mcap_sector_year = by_sector_year.market_cap_m.mean()
mcap_sect_year.loc['Basic Industries']
IPO Year
1972.0     877.240005
1973.0    1445.697371
1986.0    1396.817381
1988.0      24.847526
...
2012.0     381.796074
2013.0      22.661533
2015.0     260.075564
2016.0      81.288336
Name: market_cap_m, dtype: float64
Python으로 금융 데이터 가져오기와 관리

멀티인덱스에서 선택하기

mcap_sect_year.loc[['Basic Industries', 'Transportation']]
Sector            IPO Year
Basic Industries  1972.0      877.240005
                  1973.0     1445.697371
                  1986.0     1396.817381
                  ...
Transportation    1986.0     1176.179710
                  1991.0     6646.778622
                  1992.0       56.074572
                  ...
Python으로 금융 데이터 가져오기와 관리

연습해 봅시다!

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

Preparing Video For Download...