Manipulate a Data frame dynamically

Asked

Viewed 28 times

-1

People have a historical series of stock prices in a dataframe and need to create new dataframes of these actions grouped by sector. Dfs must without created and dynamically manipulated, here lies the challenge. I can create a new Df dynamically, but I can’t manipulate it. I summarized it in the data below.

papeis = {'VIIA3':'Varejo','LAME3':'Varejo' , 'BBDC4':'Bancos', 'VALE3':'Mineração'}
setor_ibov = [Varejo, Bancos, Mineração]
ticker_ibov = ['VIIA3','LAME4' , 'BBDC4', 'VALE3']

for i in setor_ibov:
  for j in ticker_ibov:
    if i ==  papeis1[j]:
      globals()[f"{i}"] = pd.DataFrame()
      globals()[f"{i}"] = carteira_normalizada[j]
      
      #carteiraa normalizada contém a serie histórica que preciso no 
      #novo Df do setor  
             
  • 1

    This part [f"lista{i}"]['j'] should be [f"lista{i}"][j]?????

  • In addition to the problem mentioned by @Paulomarques, this syntax globals()[f"lista{i}"] certainly could be improved, but only with the information presented in the question is it not prudent to formulate a guideline. Please [Dit] the question, submit a [mcve] and describe what you are doing and describe what you expect to get. See [Ask], [help] and do our [tour].

  • 2

    Still: Why do you want to assign specific names to Dataframes? Creating a list and putting them there would not be enough? As mentioned by @Augustovasques, it is not wise to suggest anything, just ask questions to try to understand the problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.