728x90
반응형
결과의 80가 원인의 20에서 나온다는 법칙 즉, 토지의 80%를 일부 소유자 20%가 소유하고 있다는 등의 법칙입니다.
정확히 80/20 이 아닌 소수의 원인이 다수의 결과를 가진다는 것이 핵심입니다.
그렇다고 80가 중요하지 않다는 것은 아닙니다.
임의의 데이터로 80/20 법칙을 코딩해 봅니다.
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
df = pd.DataFrame({'구매액': [71, 66, 67, 76, 75, 56, 78, 68, 67, 73]})
df.index = ['A', 'B', 'C', 'D', 'E', 'F','G', 'H', 'I', 'J']
df = df.sort_values(by='구매액', ascending=False)
df['cumperc'] = df['구매액'].cumsum()/df['구매액'].sum()*100
fig, ax = plt.subplots(figsize=(14, 6))
ax.bar(df.index, df['구매액'], color='skyblue', label='sum')
ax2 = ax.twinx()
ax2.plot(df.index, df['cumperc'], color='red', marker="D", ms=10, label='%')
ax2.yaxis.set_major_formatter(PercentFormatter())
ax.tick_params(axis='y', colors='skyblue')
ax2.tick_params(axis='y', colors='red')
plt.legend()
plt.grid()
plt.show()
빨간 라인이 % 입니다. 대략 G, D 이용자가 20& 이상 구매액을 가지고 있는 것을 알 수 있습니다.
주식시장에서 시가총액의 80/20법칙을 보겠습니다.
from pykrx import stock
import pandas as pd
import matplotlib.pyplot as plt
# 종목코드에 종목명 연결
stock_name = []
tickers = stock.get_market_ticker_list("20220819", market="KOSPI")
for ticker in stock.get_market_ticker_list():
name = stock.get_market_ticker_name(ticker)
stock_name.append([ticker, name])
df2 = pd.DataFrame(stock_name)
df2.columns = ['티커', 'name']
df = stock.get_market_cap("20220819", market="KOSPI") # 시가총액
df3 = pd.merge(left=df, right=df2, how='left', on=['티커'], sort=False)
df3 = df3.sort_values(by='시가총액', ascending=False)
df3['P'] = df3['시가총액'].cumsum()/df['시가총액'].sum()*100
%matplotlib Qt
plt.rcParams['font.family'] = 'NanumGothic'
fig, ax = plt.subplots(figsize=(14, 6))
ax.bar(df3.index, df3['시가총액'], color='steelblue', label='sum')
ax2 = ax.twinx()
ax2.plot(df3.index, df3['P'], color='red', marker="D", ms=4, label='%')
ax2.yaxis.set_major_formatter(PercentFormatter())
ax.tick_params(axis='y', colors='steelblue')
# ax.set_ylim([0, 3e15])
ax2.tick_params(axis='y', colors='red')
plt.legend()
plt.grid()
plt.show()
df3를 보면 삼성전자 만 15% 이상을 차지하기 때문에
KOSPI 전제 938개 종목 중 20%(187개)를 보겠습니다.
tot = df3['시가총액'].sum()
tot_cnt = len(df3) # 종목 총개
cnt_20 = int(tot_cnt * 0.2) # 20% 종목수
# tot_sum20 = tot * 0.2 # 20% 시가총액
tot_sum20 = df3['시가총액'][0:cnt_20].sum() # 20% 종목수의 시가총액
p = tot_sum20 * 100 / tot
# print(tot, tot_cnt, cnt_20, tot_sum20, p)
print('전체 시가총액 = ', format(tot,','))
print('전체 종목수 = ', tot_cnt)
print('20%종목수 = ',cnt_20)
print('20% 시가총액 = ', format(tot_sum20, ','))
print('20% 시가총액% = ', str(round(p,1)) + ' %')
상위 20% 종목이 전체 시가총액의 90.1 %를 차지하고 있습니다.
전체 시가총액 = 1,963,842,895,285,751
전체 종목수 = 938
20%종목수 = 187
20% 시가총액 = 1,768,955,157,507,435
20% 시가총액% = 90.1 %
728x90
반응형
'프로그램' 카테고리의 다른 글
[파이썬/법칙] 콜라츠 추측 (1) | 2022.08.21 |
---|---|
[Tensorflow] 에러 (tensorflow Fail to find the dnn implementation) (0) | 2022.08.21 |
[파이썬] 주식 종목코드, 종목명 연계 (1) | 2022.08.20 |
[파이썬] 윈도우 UI 기초 (0) | 2022.08.20 |
[윈도우] 파파고에서 음성 파일 받기 (0) | 2022.08.18 |
댓글