0

I have a list like this:

symbols = ['AAPL', 'MSFT']

I want to use the values in the list to complete the same processing 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.T
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.T
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])

Is there a way that I can create a function to pull values from the list and process?

Thanks!

1
  • What have you tried to realize this? Were there any errors? Commented Sep 14, 2020 at 13:46

1 Answer 1

1

I would 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 the list should allow you to process list with any length. If there are empty lists, you should take care of this, you might end up with empty variable concate otherwise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.