Upgrade from Eikon -> Workspace. Learn about programming differences.

For a deeper look into our Eikon Data API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
16 3 6 9

Python Dataframe aggregate/groupby columns to do calculation

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

eikoneikon-data-apipythonrefinitiv-dataplatform-eikonworkspaceworkspace-data-api
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Upvotes
Accepted
39.4k 77 11 27

@sunny.to@refinitiv.com

I think what you're looking for is the groupby method of pandas. The documentation is available following the link below
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html
But I suggest you start by reading the following user guide
https://pandas.pydata.org/docs/user_guide/groupby.html

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thanks Alex. I will try the information you provided and come back to you if encounter further questions.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.