Skip to content
Advertisement

Operation between 2 arrays for many rows based on date

I have a dataset df_1 that looks like this:

date stock A stock B stock C stock D
2020-11-01 4 8 14 30
2020-11-10 0.4 0.6 0.8 0.2
2020-11-30 6 10 20 35
2020-12-01 6 10 20 35
2020-11-31 8 12 25 0.1

And a second dataset, df_2:

date output1 output2
11/2020 stock A,stock B stock C, stock D
12/2020 stock B,stock D stock A,stock C

I want to, for every output in df_2, compute the return for each month. An expected result for this example is df_3:

date output1 output2
11/2020 stock A: 50%, stock B: 25% stock C:42.8% , stock D: 16.6%
12/2020 stock B: 20% ,stock D: 14.3% stock A: 33.3% , stock C: 25%

In the df_3, I do not need the result to be displayed with a comma separator, however, I need later to be able to plot everything in a graph with time on the X axis and stock values (from df_3) on the Y axis.

Update 1: As requested, here is the constructor of my sample dataframes: df1:

JavaScript

df2: obs: “losers” stands for “output1”

JavaScript

Update 2: I found another way around, we can use df1 to compute the monthly returns, and I would end up with this:

JavaScript
date stock A Stock B
11/2020 0.5 0.25
12/2020 0.33% 0.20

Now I need to combine this df with df2 for the desired output table, which must be easier.

Advertisement

Answer

Assuming the index of df1 is a DatetimeIndex and df2 a PeriodIndex. So suppose the following dataframes:

JavaScript

First, clean your first dataframe:

JavaScript

At this point, your first dataframe looks like:

JavaScript

Now, reshape your second dataframe:

JavaScript

At this point your second dataframe looks like:

JavaScript

Finally, merge your dataframes together:

JavaScript

The final output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement