Hi team,
Below myWACC() is calculated in a country. I would like to apply this calculation groupby its column such as TRBCEcoSectorName. below coding is show one country as output. Would how to make it output aggregate and grouping as column "TRBCEcoSectorName"? Wonder it is possible? Thanks in advance.
import asyncio
import pandas as pd
import numpy as np
from datetime import date
import plotly.express as px
import refinitiv.dataplatform.eikon as ek
import ipywidgets
from ipywidgets import *
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# by using stane along Jupyter Note book
ek.set_app_key('DEFAULT_CODE_BOOK_APP_KEY')
def my_WACC():
start_date='07/01/2021'
scale='6'
curn='USD'
Rics = country_screen = 'SCREEN(U(IN(Equity(active,public,primary))/*UNV:Public*/), IN(TR.ExchangeCountryCode,"HK"), IN(TR.InstrumentTypeCode,"ADR","BDR","CEDEAR","CHINDR","DRC","EDR","FULLPAID","GDR","INDIDR","INTERDR","ORD","PDR"),TOP(TR.CompanyMarketCap, 5000, nnumber), CURN=USD)'
df_main,err=ek.get_data(Rics,
['TR.CompanyMarketCap',
'Zav(Avail(TR.TotalDebtOutstanding(Period=FI0),TR.TotalDebtOutstanding(Period=FY0)))',
'Zav(Avail(TR.TtlPreferredSharesOut(Period=FI0),TR.TtlPreferredSharesOut(Period=FY0)))',
'TR.WACCCostofEquity',
'TR.WACCCostofDebt',
'TR.WACCCostofPreferred',
'TR.ExchangeCountryCode',
'TR.TRBCEconomicSector'],
{'Sdate':start_date,'Scale':scale,'Curn':curn})
# rename the column label
df_main.set_axis(['Instrument',
'Company_MarketCap',
'Total_debt',
'Total_PS',
'WACC_CostOfEuity',
'WACC_CostOfDebt',
'WACC_CostOfPS',
'ExchangeCode',
'TRBCEcoSectorName'],axis=1, inplace=True)
df_main['weight_mkc_country']=df_main['Company_MarketCap']/df_main['Company_MarketCap'].sum()
df_main['weight_ttd_country']=df_main['Total_debt']/df_main['Total_debt'].sum()
df_main['weight_ps_country']=df_main['Total_PS']/df_main['Total_PS'].sum()
df_main['wavg_cost_equity_country'] = df_main['WACC_CostOfEuity']*df_main['weight_mkc_country']
df_main['wavg_cost_debt_country'] = df_main['WACC_CostOfDebt']*df_main['weight_ttd_country']
df_main['wavg_cost_ps_country'] = df_main['WACC_CostOfPS']*df_main['weight_ps_country']
total_capital = df_main['Company_MarketCap'].sum()+df_main['Total_debt'].sum()+df_main['Total_PS'].sum()
weight_MktCap = df_main['Company_MarketCap'].sum()/total_capital
weight_TtlDebt = df_main['Total_debt'].sum()/total_capital
weight_ps = df_main['Total_PS'].sum()/total_capital
display(df_main)
WACC_country = weight_MktCap*(df_main['wavg_cost_equity_country'].sum()/100) + weight_TtlDebt*(df_main['wavg_cost_debt_country'].sum()/100) + weight_ps*(df_main['wavg_cost_ps_country'].sum()/100)
return WACC_country
df = my_WACC()
df