Calculation from a column from a dataframe to another dataframe based on matching index
I have 2 sets of stock data, I want to do a simple calculation to find the ratio between the close price of them. Here is my current code. The problem with this code is that the holidays are different and there for they don't always match so the dataset become desynchronized. How do I do this calculation using their index instead?
australia['SP500_ratio'] = australia["Close"]/SP500["Close"]
Example of data:
SPY:
Date Open High Low Close Adj Close Volume
4/22/2021 4170.459961 4179.569824 4123.689941 4134.97998 4134.97998 4235040000
4/26/2021 4185.029785 4194.189941 4182.359863 4187.620117 4187.620117 3738920000
4/27/2021 4188.25 4193.350098 4176.220215 4186.720215 4186.720215 3703240000
4/28/2021 4185.140137 4201.529785 4181.779785 4183.180176 4183.180176 3772390000
4/29/2021 4206.140137 4218.779785 4176.810059 4211.470215 4211.470215 4288940000
4/30/2021 4198.100098 4198.100098 4174.850098 4181.169922 4181.169922 4273680000
5/3/2021 4191.97998 4209.390137 4188.029785 4192.660156 4192.660156 4061170000
5/4/2021 4179.040039 4179.040039 4128.589844 4164.660156 4164.660156 4441080000
5/5/2021 4177.060059 4187.720215 4160.939941 4167.589844 4167.589844 4029050000
5/6/2021 4169.140137 4202.700195 4147.330078 4201.620117 4201.620117 4504860000
5/7/2021 4210.339844 4238.040039 4201.640137 4232.600098 4232.600098 4013060000
5/11/2021 4150.339844 4162.040039 4111.529785 4152.100098 4152.100098 3593110000
5/12/2021 4130.549805 4134.72998 4056.879883 4063.040039 4063.040039 3735080000
5/13/2021 4074.98999 4131.580078 4074.98999 4112.5 4112.5 3687780000
5/14/2021 4129.580078 4183.129883 4129.580078 4173.850098 4173.850098 3251920000
Australia:
Date Open High Low Close Adj Close Volume
4/22/2021 7258.899902 7312 7253.200195 7312 7312 990647800
4/23/2021 7312 7320.700195 7293.100098 7320.700195 7320.700195 814150000
4/27/2021 7307.799805 7316.5 7270.100098 7295.5 7295.5 908293800
4/28/2021 7295.600098 7334.5 7291.600098 7320 7320 962912000
4/29/2021 7320 7358.200195 7320 7346 7346 965968300
4/30/2021 7346 7346 7278.100098 7290.700195 7290.700195 966526800
5/3/2021 7290.700195 7329.600098 7280.200195 7286.799805 7286.799805 761029700
5/4/2021 7286.799805 7323.5 7286.799805 7323.5 7323.5 836158500
5/5/2021 7323.5 7371 7303.899902 7344.200195 7344.200195 853994500
5/6/2021 7344.200195 7359.799805 7287 7306 7306 1010012900
5/7/2021 7306 7345.200195 7299.700195 7325.200195 7325.200195 855567600
5/10/2021 7325.200195 7419.799805 7321 7419.799805 7419.799805 954823100
5/11/2021 7419.799805 7419.799805 7311.899902 7331.600098 7331.600098 887298800
5/13/2021 7281.100098 7281.100098 7193.200195 7209 7209 937712500
5/14/2021 7209 7281.399902 7209 7239.399902 7239.399902 825354000
I deleted a random row from both sample dataset to illustrate the desync nature of the data. Note that the row count is the same.
from Recent Questions - Stack Overflow https://ift.tt/3fynKcI
https://ift.tt/eA8V8J
Comments
Post a Comment