S&P 500 팩터 데이터 수집과 분석

이승준 [email protected]

2019-2020 FinanceData.KR

In [42]:
## FinanceDataReader 설치 (for 구글 Colab)

!pip install -q finance-datareader
In [43]:
import FinanceDataReader as fdr
import pandas as pd

pd.set_option('display.float_format', lambda x: '%.2f' % x)
In [44]:
%matplotlib inline
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (14,8)
plt.rcParams['font.size'] = 16
plt.rcParams['lines.linewidth'] = 2
plt.rcParams["axes.grid"] = True
plt.rcParams['axes.axisbelow'] = True
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams["axes.formatter.limits"] = -10000, 10000
In [45]:
# matplotlib 컬러맵 생성
import matplotlib as mpl
import numpy as np

def make_colors(n, colormap=plt.cm.Spectral):
    return colormap(np.linspace(0.1, 1.0, n))

def make_explode(n):
    explodes = np.zeros(n)
    explodes[0] = 0.15
    return explodes

S&P 500 종목 리스트

In [46]:
import FinanceDataReader as fdr

sp500 = fdr.StockListing('S&P500')
sp500.head(10)
Out[46]:
Symbol Name Sector Industry
0 MMM 3M Company Industrials Industrial Conglomerates
1 ABT Abbott Laboratories Health Care Health Care Equipment
2 ABBV AbbVie Inc. Health Care Pharmaceuticals
3 ABMD ABIOMED Inc Health Care Health Care Equipment
4 ACN Accenture plc Information Technology IT Consulting & Other Services
5 ATVI Activision Blizzard Communication Services Interactive Home Entertainment
6 ADBE Adobe Inc. Information Technology Application Software
7 AMD Advanced Micro Devices Inc Information Technology Semiconductors
8 AAP Advance Auto Parts Consumer Discretionary Automotive Retail
9 AES AES Corp Utilities Independent Power Producers & Energy Traders
In [47]:
len(sp500)
Out[47]:
505

팩터 데이터 - finviz.com

EPS, P/E 등 72개 factor

https://finviz.com/quote.ashx?t=AAPL

페이지 가져오기

페이지를 가져와서

In [48]:
import requests
from bs4 import BeautifulSoup

# 헤더를 지정하지 않으면: 403 - Forbidden: Access is denied
headers = {'User-Agent': 'Mozilla/5.0'}
r = requests.get('https://finviz.com/quote.ashx?t=AAPL', headers=headers)

soup = BeautifulSoup(r.text)
tables = soup.find_all('table')
len(tables)
Out[48]:
35

테이블 중에 calss 가 'snapshot-table2'인 것을 찾습니다.

In [49]:
snapshot_table2 = soup.find('table', attrs={'class': 'snapshot-table2'})
str(snapshot_table2)
Out[49]:
'<table border="0" cellpadding="3" cellspacing="0" class="snapshot-table2" width="100%">\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Major index membership] offsetx=[10] offsety=[20] delay=[300]" width="7%">Index</td><td align="left" class="snapshot-td2" width="8%"><b><small>DJIA S&amp;P500</small></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Price-to-Earnings (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">P/E</td><td align="left" class="snapshot-td2" width="8%"><b>27.42</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Diluted EPS (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS (ttm)</td><td align="left" class="snapshot-td2" width="8%"><b>12.76</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Insider ownership] offsetx=[10] offsety=[20] delay=[300]" width="7%">Insider Own</td><td align="left" class="snapshot-td2" width="8%"><b>0.10%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Shares outstanding] offsetx=[10] offsety=[20] delay=[300]" width="7%">Shs Outstand</td><td align="left" class="snapshot-td2" width="8%"><b>4.36B</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (Week)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Perf Week</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">3.22%</span></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Market capitalization] offsetx=[10] offsety=[20] delay=[300]" width="7%">Market Cap</td><td align="left" class="snapshot-td2" width="8%"><b>1524.52B</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Forward Price-to-Earnings (next fiscal year)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Forward P/E</td><td align="left" class="snapshot-td2" width="8%"><b>23.53</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[EPS estimate for next year] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS next Y</td><td align="left" class="snapshot-td2" width="8%"><b>14.86</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Insider transactions (6-Month change in Insider Ownership)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Insider Trans</td><td align="left" class="snapshot-td2" width="8%"><b>-2.18%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Shares float] offsetx=[10] offsety=[20] delay=[300]" width="7%">Shs Float</td><td align="left" class="snapshot-td2" width="8%"><b>4.33B</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (Month)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Perf Month</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">9.55%</span></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Income (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Income</td><td align="left" class="snapshot-td2" width="8%"><b>57.22B</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Price-to-Earnings-to-Growth] offsetx=[10] offsety=[20] delay=[300]" width="7%">PEG</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">2.39</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[EPS estimate for next quarter] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS next Q</td><td align="left" class="snapshot-td2" width="8%"><b>2.00</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Institutional ownership] offsetx=[10] offsety=[20] delay=[300]" width="7%">Inst Own</td><td align="left" class="snapshot-td2" width="8%"><b>62.00%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Short interest share] offsetx=[10] offsety=[20] delay=[300]" width="7%">Short Float</td><td align="left" class="snapshot-td2" width="8%"><b>0.79%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (Quarter)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Perf Quarter</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">52.56%</span></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Revenue (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Sales</td><td align="left" class="snapshot-td2" width="8%"><b>267.98B</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Price-to-Sales (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">P/S</td><td align="left" class="snapshot-td2" width="8%"><b>5.69</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[EPS growth this year] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS this Y</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">-2.60%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Institutional transactions (3-Month change in Institutional Ownership)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Inst Trans</td><td align="left" class="snapshot-td2" width="8%"><b>-0.62%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Short interest ratio] offsetx=[10] offsety=[20] delay=[300]" width="7%">Short Ratio</td><td align="left" class="snapshot-td2" width="8%"><b>0.85</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (Half Year)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Perf Half Y</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">25.02%</span></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Book value per share (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Book/sh</td><td align="left" class="snapshot-td2" width="8%"><b>17.99</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Price-to-Book (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">P/B</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">19.44</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[EPS growth next year] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS next Y</td><td align="left" class="snapshot-td2" width="8%"><b>19.92%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Return on Assets (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">ROA</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">17.30%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Analysts\' mean target price] offsetx=[10] offsety=[20] delay=[300]" width="7%">Target Price</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">331.03</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (Year)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Perf Year</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">75.33%</span></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Cash per share (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Cash/sh</td><td align="left" class="snapshot-td2" width="8%"><b>21.58</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Price to cash per share (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">P/C</td><td align="left" class="snapshot-td2" width="8%"><b>16.21</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Long term annual growth estimate (5 years)] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS next 5Y</td><td align="left" class="snapshot-td2" width="8%"><b>11.48%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Return on Equity (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">ROE</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">64.50%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[52-Week trading range] offsetx=[10] offsety=[20] delay=[300]" width="7%">52W Range</td><td align="left" class="snapshot-td2" width="8%"><b><small>192.58 - 355.40</small></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (Year To Date)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Perf YTD</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">19.09%</span></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Dividend (annual)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Dividend</td><td align="left" class="snapshot-td2" width="8%"><b>3.28</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Price to Free Cash Flow (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">P/FCF</td><td align="left" class="snapshot-td2" width="8%"><b>28.98</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Annual EPS growth past 5 years] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS past 5Y</td><td align="left" class="snapshot-td2" width="8%"><b>13.00%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Return on Investment (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">ROI</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">26.90%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Distance from 52-Week High] offsetx=[10] offsety=[20] delay=[300]" width="7%">52W High</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">-1.60%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Beta] offsetx=[10] offsety=[20] delay=[300]" width="7%">Beta</td><td align="left" class="snapshot-td2" width="8%"><b>1.18</b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Dividend yield (annual)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Dividend %</td><td align="left" class="snapshot-td2" width="8%"><b>0.94%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Quick Ratio (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Quick Ratio</td><td align="left" class="snapshot-td2" width="8%"><b>1.50</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Annual sales growth past 5 years] offsetx=[10] offsety=[20] delay=[300]" width="7%">Sales past 5Y</td><td align="left" class="snapshot-td2" width="8%"><b>7.30%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Gross Margin (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Gross Margin</td><td align="left" class="snapshot-td2" width="8%"><b>38.10%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Distance from 52-Week Low] offsetx=[10] offsety=[20] delay=[300]" width="7%">52W Low</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">81.60%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Average True Range (14)] offsetx=[10] offsety=[20] delay=[300]" width="7%">ATR</td><td align="left" class="snapshot-td2" width="8%"><b>8.56</b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Full time employees] offsetx=[10] offsety=[20] delay=[300]" width="7%">Employees</td><td align="left" class="snapshot-td2" width="8%"><b>137000</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Current Ratio (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Current Ratio</td><td align="left" class="snapshot-td2" width="8%"><b>1.50</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Quarterly revenue growth (yoy)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Sales Q/Q</td><td align="left" class="snapshot-td2" width="8%"><b>0.50%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Operating Margin (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Oper. Margin</td><td align="left" class="snapshot-td2" width="8%"><b>24.50%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Relative Strength Index] offsetx=[10] offsety=[20] delay=[300]" width="7%">RSI (14)</td><td align="left" class="snapshot-td2" width="8%"><b>69.12</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Volatility (Week, Month)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Volatility</td><td align="left" class="snapshot-td2" width="8%"><b><small>2.43% 2.32%</small></b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Stock has options trading on a market exchange] offsetx=[10] offsety=[20] delay=[300]" width="7%">Optionable</td><td align="left" class="snapshot-td2" width="8%"><b>Yes</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Total Debt to Equity (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Debt/Eq</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">1.40</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Quarterly earnings growth (yoy)] offsetx=[10] offsety=[20] delay=[300]" width="7%">EPS Q/Q</td><td align="left" class="snapshot-td2" width="8%"><b>3.80%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Net Profit Margin (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Profit Margin</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">21.40%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Relative volume] offsetx=[10] offsety=[20] delay=[300]" width="7%">Rel Volume</td><td align="left" class="snapshot-td2" width="8%"><b>1.62</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Previous close] offsetx=[10] offsety=[20] delay=[300]" width="7%">Prev Close</td><td align="left" class="snapshot-td2" width="8%"><b>351.73</b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Stock available to sell short] offsetx=[10] offsety=[20] delay=[300]" width="7%">Shortable</td><td align="left" class="snapshot-td2" width="8%"><b>Yes</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Long Term Debt to Equity (mrq)] offsetx=[10] offsety=[20] delay=[300]" width="7%">LT Debt/Eq</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">1.14</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Earnings date&lt;br&gt;&lt;br&gt;BMO = Before Market Open&lt;br&gt;AMC = After Market Close] offsetx=[10] offsety=[20] delay=[300]" width="7%">Earnings</td><td align="left" class="snapshot-td2" width="8%"><b>Apr 30 AMC</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Dividend Payout Ratio (ttm)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Payout</td><td align="left" class="snapshot-td2" width="8%"><b>23.90%</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Average volume (3 month)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Avg Volume</td><td align="left" class="snapshot-td2" width="8%"><b>40.32M</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Current stock price] offsetx=[10] offsety=[20] delay=[300]" width="7%">Price</td><td align="left" class="snapshot-td2" width="8%"><b>349.72</b></td>\n</tr>\n<tr class="table-dark-row">\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Analysts\' mean recommendation (1=Buy 5=Sell)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Recom</td><td align="left" class="snapshot-td2" width="8%"><b>2.00</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Distance from 20-Day Simple Moving Average] offsetx=[10] offsety=[20] delay=[300]" width="7%">SMA20</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">5.43%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Distance from 50-Day Simple Moving Average] offsetx=[10] offsety=[20] delay=[300]" width="7%">SMA50</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">13.56%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Distance from 200-Day Simple Moving Average] offsetx=[10] offsety=[20] delay=[300]" width="7%">SMA200</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#008800;">26.01%</span></b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Volume] offsetx=[10] offsety=[20] delay=[300]" width="7%">Volume</td><td align="left" class="snapshot-td2" width="8%"><b>65,261,525</b></td>\n<td align="left" class="snapshot-td2-cp" title="cssbody=[tooltip_short_bdy] cssheader=[tooltip_short_hdr] body=[Performance (today)] offsetx=[10] offsety=[20] delay=[300]" width="7%">Change</td><td align="left" class="snapshot-td2" width="8%"><b><span style="color:#aa0000;">-0.57%</span></b></td>\n</tr>\n</table>'

테이블 HTML을 DataFrame으로 변환하는 가장 간단한 방법은 pd.read_html()을 사용하는 것입니다.

pd.read_html()은 항상 DataFrame 리스트를 반환합니다. 따라서 리스트의 첫 번째 요소를 가져옵니다.

In [50]:
tables = pd.read_html(str(snapshot_table2))
df = tables[0]
df
Out[50]:
0 1 2 3 4 5 6 7 8 9 10 11
0 Index DJIA S&P500 P/E 27.42 EPS (ttm) 12.76 Insider Own 0.10% Shs Outstand 4.36B Perf Week 3.22%
1 Market Cap 1524.52B Forward P/E 23.53 EPS next Y 14.86 Insider Trans -2.18% Shs Float 4.33B Perf Month 9.55%
2 Income 57.22B PEG 2.39 EPS next Q 2.00 Inst Own 62.00% Short Float 0.79% Perf Quarter 52.56%
3 Sales 267.98B P/S 5.69 EPS this Y -2.60% Inst Trans -0.62% Short Ratio 0.85 Perf Half Y 25.02%
4 Book/sh 17.99 P/B 19.44 EPS next Y 19.92% ROA 17.30% Target Price 331.03 Perf Year 75.33%
5 Cash/sh 21.58 P/C 16.21 EPS next 5Y 11.48% ROE 64.50% 52W Range 192.58 - 355.40 Perf YTD 19.09%
6 Dividend 3.28 P/FCF 28.98 EPS past 5Y 13.00% ROI 26.90% 52W High -1.60% Beta 1.18
7 Dividend % 0.94% Quick Ratio 1.50 Sales past 5Y 7.30% Gross Margin 38.10% 52W Low 81.60% ATR 8.56
8 Employees 137000 Current Ratio 1.50 Sales Q/Q 0.50% Oper. Margin 24.50% RSI (14) 69.12 Volatility 2.43% 2.32%
9 Optionable Yes Debt/Eq 1.40 EPS Q/Q 3.80% Profit Margin 21.40% Rel Volume 1.62 Prev Close 351.73
10 Shortable Yes LT Debt/Eq 1.14 Earnings Apr 30 AMC Payout 23.90% Avg Volume 40.32M Price 349.72
11 Recom 2.00 SMA20 5.43% SMA50 13.56% SMA200 26.01% Volume 65261525 Change -0.57%

컬럼 이름을 지정합니다. key value 조합이 6번 반복됩니다.

In [51]:
df.columns = ['key', 'value'] * 6
df
Out[51]:
key value key value key value key value key value key value
0 Index DJIA S&P500 P/E 27.42 EPS (ttm) 12.76 Insider Own 0.10% Shs Outstand 4.36B Perf Week 3.22%
1 Market Cap 1524.52B Forward P/E 23.53 EPS next Y 14.86 Insider Trans -2.18% Shs Float 4.33B Perf Month 9.55%
2 Income 57.22B PEG 2.39 EPS next Q 2.00 Inst Own 62.00% Short Float 0.79% Perf Quarter 52.56%
3 Sales 267.98B P/S 5.69 EPS this Y -2.60% Inst Trans -0.62% Short Ratio 0.85 Perf Half Y 25.02%
4 Book/sh 17.99 P/B 19.44 EPS next Y 19.92% ROA 17.30% Target Price 331.03 Perf Year 75.33%
5 Cash/sh 21.58 P/C 16.21 EPS next 5Y 11.48% ROE 64.50% 52W Range 192.58 - 355.40 Perf YTD 19.09%
6 Dividend 3.28 P/FCF 28.98 EPS past 5Y 13.00% ROI 26.90% 52W High -1.60% Beta 1.18
7 Dividend % 0.94% Quick Ratio 1.50 Sales past 5Y 7.30% Gross Margin 38.10% 52W Low 81.60% ATR 8.56
8 Employees 137000 Current Ratio 1.50 Sales Q/Q 0.50% Oper. Margin 24.50% RSI (14) 69.12 Volatility 2.43% 2.32%
9 Optionable Yes Debt/Eq 1.40 EPS Q/Q 3.80% Profit Margin 21.40% Rel Volume 1.62 Prev Close 351.73
10 Shortable Yes LT Debt/Eq 1.14 Earnings Apr 30 AMC Payout 23.90% Avg Volume 40.32M Price 349.72
11 Recom 2.00 SMA20 5.43% SMA50 13.56% SMA200 26.01% Volume 65261525 Change -0.57%

컬럼을 행으로 만들기

In [52]:
df_list = [df.iloc[:, i*2: i*2+2] for i in range(6)]
df_factor = pd.concat(df_list, ignore_index=True)

df_factor.set_index('key', inplace=True)
df_factor.head(20)
Out[52]:
value
key
Index DJIA S&P500
Market Cap 1524.52B
Income 57.22B
Sales 267.98B
Book/sh 17.99
Cash/sh 21.58
Dividend 3.28
Dividend % 0.94%
Employees 137000
Optionable Yes
Shortable Yes
Recom 2.00
P/E 27.42
Forward P/E 23.53
PEG 2.39
P/S 5.69
P/B 19.44
P/C 16.21
P/FCF 28.98
Quick Ratio 1.50
In [53]:
df_factor.tail(10)
Out[53]:
value
key
Perf Quarter 52.56%
Perf Half Y 25.02%
Perf Year 75.33%
Perf YTD 19.09%
Beta 1.18
ATR 8.56
Volatility 2.43% 2.32%
Prev Close 351.73
Price 349.72
Change -0.57%
In [54]:
len(df_factor)
Out[54]:
72

주요 팩터

70개가 넘는 팩터 데이터를 얻을 수 있습니다. 이 중 주요한 팩터 데이터를 살펴봅니다.

  • Market Cap
  • Dividend yield
  • PER
  • PBR
  • ROE
  • Beta
In [55]:
v = df_factor.value

marcap = v['Market Cap']
dividend = v['Dividend %']
per = v['P/E']
pbr = v['P/B']
beta = v['Beta']
roe = v['ROE']

marcap, dividend, per, pbr, beta, roe 
Out[55]:
('1524.52B', '0.94%', '27.42', '19.44', '1.18', '64.50%')

데이터 전처리

  1. 단위(B, M, K) 수치값으로 변환
  2. '%' 삭제
  3. float 타입 변환
In [56]:
import re

def _conv_to_float(s):
    if s[-1] == '%':
        s = s.replace('%', '')
    if s[-1] in list('BMK'):
        powers = {'B': 10 ** 9, 'M': 10 ** 6, 'K': 10 ** 3, '': 1}
        m = re.search("([0-9\.]+)(M|B|K|)", s)
        if m:
            val, mag = m.group(1), m.group(2)
            return float(val) * powers[mag]
    try:
        result = float(s)
    except:
        result = None
    return result
In [57]:
marcap = _conv_to_float(marcap)
dividend = _conv_to_float(dividend)
per = _conv_to_float(per)
pbr = _conv_to_float(pbr)
beta = _conv_to_float(beta)
roe = _conv_to_float(roe)

marcap, dividend, per, pbr, beta, roe  
Out[57]:
(1524520000000.0, 0.94, 27.42, 19.44, 1.18, 64.5)

팩터 크롤링, 함수로 정리하기

In [58]:
import pandas as pd
import re

## 데이터 전처리 변환
def _conv_to_float(s):
    if s[-1] == '%':
        s = s.replace('%', '')
    if s[-1] in list('BMK'):
        powers = {'B': 10 ** 9, 'M': 10 ** 6, 'K': 10 ** 3, '': 1}
        m = re.search("([0-9\.]+)(M|B|K|)", s)
        if m:
            val, mag = m.group(1), m.group(2)
            return float(val) * powers[mag]
    try:
        result = float(s)
    except:
        result = None
    return result

def stock_factors(sym):
    headers = {'User-Agent': 'Mozilla/5.0'}
    r = requests.get('https://finviz.com/quote.ashx?t=AAPL', headers=headers)
    snapshot_table2 = soup.find('table', attrs={'class': 'snapshot-table2'})
    tables = pd.read_html(str(snapshot_table2))
    df = tables[0]
    df.columns = ['key', 'value'] * 6

    ## 컬럼을 행으로 만들기
    df_list = [df.iloc[:, i*2: i*2+2] for i in range(6)]
    df_factor = pd.concat(df_list, ignore_index=True)
    df_factor.set_index('key', inplace=True)

    v = df_factor.value
    marcap = _conv_to_float(v['Market Cap'])
    dividend = _conv_to_float(v['Dividend %'])
    per = _conv_to_float(v['P/E'])
    pbr = _conv_to_float(v['P/B'])
    beta = _conv_to_float(v['Beta'])
    roe = _conv_to_float(v['ROE'])

    return {'MarCap':marcap, 'Dividend':dividend, 'PER':per, 'PBR':pbr, 'Beta':beta, 'ROE':roe}
In [59]:
stock_factors('AAPL')
Out[59]:
{'Beta': 1.18,
 'Dividend': 0.94,
 'MarCap': 1524520000000.0,
 'PBR': 19.44,
 'PER': 27.42,
 'ROE': 64.5}
In [60]:
stock_factors('NFLX')
Out[60]:
{'Beta': 1.18,
 'Dividend': 0.94,
 'MarCap': 1524520000000.0,
 'PBR': 19.44,
 'PER': 27.42,
 'ROE': 64.5}

전종목 팩터 데이터 수집과 저장(JSON)

In [61]:
# 디렉토리가 없으면 생성
import os

folder = "sp500_factors/"

if not os.path.isdir(folder):
    os.mkdir(folder)
In [ ]:
import json
import pandas as pd

re_map_sym = {'BRKB': 'BRK-B', 'BR': 'BRK-A', 'BFB':'BF-B'}

for ix, row in sp500.iterrows(): # 종목 10개만 진행해 봅니다.
    sym, name = row['Symbol'], row['Name']
    json_fn = folder + '%s.json' % (sym)
    if os.path.exists(json_fn):
        print('skip', json_fn)
        continue

    if sym in re_map_sym:
        sym = re_map_sym[sym]
    factors = stock_factors(sym)
    with open(json_fn, 'w') as f:
        json.dump(factors, f)
    print(sym, name)

데이터 다운로드

데이터 다운로드에 시간이 많이 걸리는 경우 다음 명령으로 미리 받아둔 데이터로 진행 합니다.

!curl -L -o sp500_factors.zip "https://www.dropbox.com/s/43g1uosf4s505ks/sp500_factors_20190317.zip?dl=0"
!unzip -q -o sp500_factors.zip

JSON 팩터 데이터 읽기

In [63]:
# JSON 팩터 데이터 읽기
for ix, row in sp500.iterrows():
    sym, name = row['Symbol'], row['Name']
    json_fn = folder + '%s.json' % (sym)

    with open(json_fn, 'r') as f:
        factors = json.load(f)
        
        for f in ['MarCap', 'Dividend', 'PER', 'PBR', 'Beta', 'ROE']:
            sp500.loc[ix,f] = factors[f]
In [64]:
sp500.head(20)
Out[64]:
Symbol Name Sector Industry MarCap Dividend PER PBR Beta ROE
0 MMM 3M Company Industrials Industrial Conglomerates 120830000000.00 2.77 22.64 12.33 1.05 51.70
1 ABT Abbott Laboratories Health Care Health Care Equipment 140580000000.00 1.60 57.62 4.61 1.13 7.70
2 ABBV AbbVie Inc. Health Care Pharmaceuticals 121000000000.00 5.26 22.37 nan 1.15 -202.20
3 ABMD ABIOMED Inc Health Care Health Care Equipment 15180000000.00 nan 69.51 17.63 0.39 29.00
4 ACN Accenture plc Information Technology IT Consulting & Other Services 106170000000.00 1.75 24.53 8.38 1.10 39.60
5 ATVI Activision Blizzard Communication Services Interactive Home Entertainment 34140000000.00 0.83 18.96 3.01 0.83 17.20
6 ADBE Adobe Inc. Information Technology Application Software 126100000000.00 nan 46.07 13.41 1.11 29.10
7 AMD Advanced Micro Devices Inc Information Technology Semiconductors 23490000000.00 nan 72.55 18.34 3.41 33.80
8 AAP Advance Auto Parts Consumer Discretionary Automotive Retail 11270000000.00 0.15 27.38 3.18 1.01 11.80
9 AES AES Corp Utilities Independent Power Producers & Energy Traders 12110000000.00 3.00 11.88 3.80 1.06 37.00
10 AFL AFLAC Inc Financials Life & Health Insurance 37540000000.00 2.16 13.24 1.62 0.78 12.30
11 A Agilent Technologies Inc Health Care Health Care Equipment 26000000000.00 0.81 22.61 5.12 1.39 24.30
12 APD Air Products & Chemicals Inc Materials Industrial Gases 40650000000.00 2.51 24.57 3.74 0.83 15.80
13 AKAM Akamai Technologies Inc Information Technology Internet Services & Infrastructure 11860000000.00 nan 40.42 3.70 0.55 9.10
14 ALK Alaska Air Group Inc Industrials Airlines 6910000000.00 2.51 15.82 1.83 0.84 12.00
15 ALB Albemarle Corp Materials Specialty Chemicals 9210000000.00 1.72 14.02 2.53 1.69 19.00
16 ARE Alexandria Real Estate Equities Real Estate Office REITs 15740000000.00 2.76 40.37 2.05 0.94 5.50
17 ALXN Alexion Pharmaceuticals Health Care Biotechnology 30390000000.00 nan 1767.40 3.32 1.58 0.90
18 ALGN Align Technology Health Care Health Care Supplies 20180000000.00 nan 51.27 16.09 2.15 33.40
19 ALLE Allegion Industrials Building Products 8350000000.00 1.22 20.47 12.87 1.19 78.70

섹터별 종목수

In [65]:
sector_count = sp500.groupby('Sector')['Symbol'].count().sort_values(ascending=False)
sector_count
Out[65]:
Sector
Industrials               72
Information Technology    71
Financials                66
Consumer Discretionary    63
Health Care               61
Consumer Staples          33
Real Estate               31
Utilities                 28
Materials                 28
Energy                    26
Communication Services    26
Name: Symbol, dtype: int64
In [66]:
# sector_count 섹터별 종목수
values = sector_count.values
labels = sector_count.index

n = len(labels)
plt.pie(values, labels=labels, colors=make_colors(n), explode=make_explode(n), autopct='%1.1f%%', shadow=True, startangle=135)
plt.axis('equal')
plt.show()

섹터별 시가총액

In [67]:
sector_marcap = sp500.groupby('Sector')['MarCap'].sum().sort_values(ascending=False)
sector_marcap
Out[67]:
Sector
Industrials              15837850000000.00
Information Technology   14246240000000.00
Financials                9362130000000.00
Communication Services    8010320000000.00
Health Care               7998570000000.00
Consumer Discretionary    7129190000000.00
Materials                 6614570000000.00
Energy                    2785170000000.00
Real Estate               2227780000000.00
Consumer Staples          1984570000000.00
Utilities                  786520000000.00
Name: MarCap, dtype: float64
In [68]:
# sector_marcap 시가총액
values = sector_marcap.values
labels = sector_marcap.index

n = len(labels)

plt.pie(values, labels=labels, colors=make_colors(n), explode=make_explode(n), autopct='%1.1f%%', shadow=True, startangle=135)
plt.axis('equal')
plt.show()
In [69]:
_ = sector_marcap.plot(kind='bar', color='orange', alpha=0.7)

섹터별 팩터 분석

In [70]:
sp500.groupby('Sector').describe()['PER'].sort_values('mean', ascending=False)
Out[70]:
count mean std min 25% 50% 75% max
Sector
Consumer Discretionary 59.00 210.94 1442.85 6.65 13.12 20.27 25.82 11105.00
Health Care 55.00 69.12 234.88 14.55 21.05 31.26 47.51 1767.40
Real Estate 31.00 61.17 98.04 11.70 27.04 37.18 58.99 573.42
Information Technology 70.00 36.75 49.48 3.29 17.76 27.38 37.45 400.34
Communication Services 23.00 27.50 27.25 6.94 14.80 20.74 27.42 137.96
Utilities 26.00 23.96 6.82 11.88 20.56 22.80 25.47 41.86
Materials 28.00 22.77 11.16 7.24 12.41 24.11 27.90 54.15
Industrials 70.00 22.54 9.72 9.02 16.44 20.90 27.42 56.83
Consumer Staples 30.00 22.49 8.73 7.39 14.45 22.13 28.68 43.70
Financials 64.00 19.57 18.27 7.13 11.19 13.55 23.39 125.34
Energy 21.00 18.34 8.94 8.23 12.23 15.32 24.06 43.83
In [71]:
sp500[sp500['Sector']=='Consumer Discretionary'].sort_values(by='PER', ascending=False)[:10]
Out[71]:
Symbol Name Sector Industry MarCap Dividend PER PBR Beta ROE
456 UAA Under Armour (Class A) Consumer Discretionary Apparel, Accessories & Luxury Goods 9510000000.00 nan 11105.00 4.91 0.44 -2.30
102 CMG Chipotle Mexican Grill Consumer Discretionary Restaurants 17480000000.00 nan 105.03 12.31 0.64 12.50
26 AMZN Amazon.com Inc. Consumer Discretionary Internet & Direct Marketing Retail 843780000000.00 nan 86.33 19.27 1.63 27.00
495 WYNN Wynn Resorts Ltd Consumer Discretionary Casinos & Gaming 12370000000.00 2.59 73.85 6.13 1.69 36.60
181 EXPE Expedia Group Consumer Discretionary Internet & Direct Marketing Retail 18160000000.00 1.05 45.98 4.40 0.95 9.80
225 HAS Hasbro Inc. Consumer Discretionary Leisure Products 10870000000.00 3.14 42.57 6.26 0.89 12.60
342 NKE Nike Consumer Discretionary Apparel, Accessories & Luxury Goods 137350000000.00 1.01 33.77 15.72 0.68 23.10
232 HLT Hilton Worldwide Holdings Inc Consumer Discretionary Hotels, Resorts & Cruise Lines 25260000000.00 0.70 33.58 45.62 1.17 89.30
317 MGM MGM Resorts International Consumer Discretionary Casinos & Gaming 13940000000.00 2.00 32.73 2.11 1.40 6.50
425 SBUX Starbucks Corp. Consumer Discretionary Restaurants 89470000000.00 2.04 31.12 nan 0.49 173.40

Outlier: UAA(언더 아머) - 스포츠 용품 브랜드, 기어라는 기능성 소재로 유명

In [72]:
sp500.groupby('Sector').describe()['PBR'].sort_values('mean', ascending=False)
Out[72]:
count mean std min 25% 50% 75% max
Sector
Industrials 69.00 17.58 76.86 1.55 2.97 4.99 10.19 642.36
Consumer Discretionary 57.00 10.89 23.38 0.93 2.21 4.54 9.20 157.09
Information Technology 66.00 9.53 7.52 1.23 3.76 7.48 13.30 44.20
Consumer Staples 29.00 6.95 6.32 0.60 2.32 4.83 9.19 27.75
Health Care 58.00 6.89 6.03 1.05 2.82 4.87 9.03 29.99
Materials 27.00 6.20 6.15 0.82 2.37 3.33 7.22 19.44
Communication Services 26.00 6.11 7.37 0.57 2.21 3.45 5.50 30.12
Financials 64.00 5.36 13.77 0.00 1.18 1.62 2.48 80.63
Real Estate 30.00 4.60 4.54 1.34 2.10 2.90 4.77 19.44
Energy 26.00 2.57 3.59 0.72 1.22 1.71 2.30 19.44
Utilities 27.00 2.30 0.56 1.46 2.00 2.17 2.48 3.80
In [73]:
sp500.groupby('Sector').describe()['ROE'].sort_values('mean', ascending=False)
Out[73]:
count mean std min 25% 50% 75% max
Sector
Industrials 70.00 39.23 99.96 -215.10 16.88 27.85 42.65 785.10
Consumer Discretionary 61.00 37.00 92.71 -100.30 12.50 23.00 36.60 609.20
Financials 63.00 31.37 70.48 0.00 9.95 12.80 19.25 402.10
Communication Services 22.00 27.03 21.70 -13.90 16.75 21.55 29.25 64.50
Information Technology 69.00 26.57 58.04 -274.60 13.00 29.50 47.70 172.20
Materials 28.00 24.06 24.12 -50.10 10.15 21.15 31.45 64.50
Health Care 58.00 16.78 37.89 -202.20 7.78 17.30 28.35 92.60
Real Estate 31.00 14.14 16.97 -1.60 4.95 8.20 16.25 72.80
Energy 26.00 13.93 14.02 -3.30 4.78 12.40 18.85 64.50
Utilities 27.00 8.61 12.01 -40.10 6.55 10.10 11.30 37.00
Consumer Staples 32.00 -0.73 142.66 -736.20 9.25 22.45 38.33 109.30
In [74]:
sp500.groupby('Sector').describe()['Beta'].sort_values('mean', ascending=False)
Out[74]:
count mean std min 25% 50% 75% max
Sector
Energy 25.00 1.27 0.42 0.60 1.00 1.18 1.37 2.26
Information Technology 71.00 1.23 0.41 0.55 1.01 1.18 1.36 3.41
Materials 27.00 1.22 0.43 0.04 0.99 1.18 1.51 2.28
Industrials 71.00 1.18 0.29 0.47 1.04 1.18 1.31 2.52
Financials 64.00 1.17 0.35 0.34 0.95 1.18 1.42 2.04
Health Care 61.00 1.06 0.30 0.32 0.89 1.10 1.21 2.15
Communication Services 22.00 1.04 0.31 0.49 0.84 1.06 1.18 1.58
Consumer Discretionary 62.00 1.02 0.39 0.11 0.71 1.04 1.27 1.90
Real Estate 31.00 0.75 0.38 0.19 0.51 0.66 0.94 1.71
Consumer Staples 32.00 0.62 0.30 0.12 0.41 0.64 0.77 1.55
Utilities 28.00 0.32 0.22 -0.04 0.18 0.28 0.36 1.06
In [75]:
sp500.groupby('Sector').describe()['Dividend'].sort_values('mean', ascending=False)
Out[75]:
count mean std min 25% 50% 75% max
Sector
Real Estate 29.00 3.60 1.25 0.94 2.86 3.57 3.92 6.99
Utilities 28.00 3.15 0.91 0.29 2.87 3.06 3.70 5.04
Consumer Staples 32.00 2.84 1.22 0.98 2.10 2.66 3.57 5.64
Communication Services 15.00 2.57 2.34 0.69 0.94 1.57 3.79 8.27
Energy 26.00 2.55 1.61 0.35 1.10 2.40 3.97 5.52
Consumer Discretionary 50.00 2.43 1.45 0.14 1.35 2.33 3.39 7.12
Financials 63.00 2.36 1.07 0.52 1.56 2.36 2.95 6.07
Information Technology 51.00 2.01 1.18 0.03 1.14 1.75 2.71 5.29
Materials 28.00 2.00 1.19 0.35 1.04 1.73 2.56 4.85
Industrials 67.00 1.73 0.90 0.15 0.97 1.72 2.17 5.19
Health Care 38.00 1.62 1.25 0.02 0.72 1.21 2.33 5.26

데이터 다운로드 (구글 Colab)

In [ ]:
!zip -r sp500_factors.zip sp500_factors
In [80]:
from google.colab import files
files.download('sp500_factors.zip')

2020 FinanceData.KR