Creating new dataframes and using merge

Asked

Viewed 49 times

0

I used yahooquery to import a historical data series of 100 exchange-traded assets. I created a dataframe with this data for a period of 6 months:

df = tickers.history(period='6mo', interval="1d")

I only need the closing quotation, so I created a dataframe called dfclose with this data and used the assets as index.

dfclose = df.adjclose
dfclose = dfclose.reset_index(level=['symbol', 'date'])

I need to separate the assets from the ones not in the same column. The solution I found was to create a data frame for each asset and then merge them using the merge function.

df_AALR = dfclose[dfclose.symbol=='AALR3.SA']
df_YDUQ = dfclose[dfclose.symbol=='YDUQ3.SA']
df_PETR = dfclose[dfclose.symbol=='PETR4.SA']
pd.merge(df_AALR,df_YDUQ, on=['date']).merge(df_PETR, on='date')

I’m not sure that’s the best solution. I think it would be possible to create a function to create the dataframes without having to type each one and then to join them.

From this final dataframe, I could proceed with the analyses. Am I on the right track? How to create this function?

  • on pandas, when you want to create a mask to filter the data using more than one condition you can use & (for and) and | (for or). I don’t have your Dataframe to check, but I believe you could do something like dfclose[dfclose.symbol=='AALR3.SA' | dfclose.symbol=='YDUQ3.SA']

  • Failed. Error message: Typeerror: Unsupported operand type(s) for |: str and 'bool'

  • Then there is something wrong because the two have to be bool. You can copy here as you tested?

  • df_final = dfclose[dfclose.Symbol=='ALR3.SA']|dfclose[dfclose.Symbol=='YDUQ3.SA']

  • That’s the mistake, you did it differently than I suggested. Copy this df_final = dfclose[dfclose.symbol=='AALR3.SA' | dfclose.symbol=='YDUQ3.SA']

  • Thanks but still giving error: Cannot perform 'ror_' with a dtyped [Object] array and scalar of type [bool]

  • I managed using the pd.pivot_table function.

Show 2 more comments
No answers

Browser other questions tagged

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