Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (18.4k points)

I have the list like this:

symbols = ['AAPL', 'MSFT']

I want to use values in a list to complete the process as below.

df_AAPL_income = get_annual_finData_by_symbol('income','AAPL','us')

df_AAPL_balancesheet = get_annual_finData_by_symbol('balancesheet','AAPL','us')

df_MSFT_income = get_annual_finData_by_symbol('income','MSFT','us')

df_MSFT_balancesheet = get_annual_finData_by_symbol('balancesheet','MSFT','us')

MSFT = calculateMetrics('df_'+'MSFT'+'_balancesheet','df_'+'MSFT'+'_income')


MSFT = MSFT.reset_index()

MSFT = MSFT.rename(columns={'breakdown': 'fiscal_year'})

MSFT.insert(0, 'Ticker', 'MSFT')

MSFT = MSFT.set_index(['Ticker','fiscal_year'])

AAPL = calculateMetrics(df_AAPL_balancesheet,df_AAPL_income)


AAPL = AAPL.reset_index()

AAPL = AAPL.rename(columns={'breakdown': 'fiscal_year'})

AAPL.insert(0, 'Ticker', 'AAPL')

AAPL = AAPL.set_index(['Ticker','fiscal_year'])

concate = pd.concat([AAPL, MSFT])

can create the function to pull values from the list and process?

1 Answer

0 votes
by (36.8k points)

You can do it like this:

symbols = ['AAPL', 'MSFT']

def process_symbol(symbol: str):

  df_income= get_annual_finData_by_symbol('income',symbol,'us')

  df_balancesheet = get_annual_finData_by_symbol('balancesheet',symbol,'us')

  ret = calculateMetrics(df_balancesheet,df_income)

  ret = ret.T

  ret = ret.reset_index()

  ret = ret.rename(columns={'breakdown': 'fiscal_year'})

  ret.insert(0, 'Ticker', symbol)

  ret = ret.set_index(['Ticker','fiscal_year'])

  return ret

concate = pd.DataFrame(columns = ['Ticker','fiscal_year']) //define it with header you need

for symbol in symbols:

  what_i_need = process_symbol(symbol)

  concate = pd.concat([concate, what_i_need])

Looping over a list should allow you to process the list with any length. If there are empty lists then you should take care of this, you might end up with an empty variable.

Do check out Data Science with Python course which helps you understand from scratch

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers


104k users

Browse Categories