'how can I loop through a column in pandas data frame and append the results in another column
working on the following stock data, I am trying to calculate the exponential moving average based on the following equation: ema= (close pricealpha factor)+ alpha factorprevious value of moving average) the first value of the moving average will be the first close price. I need to store the data in a column named ema. please see my data below: I tried the ewm function, but the results are not satisfying and I want to use the equation used by excel data analysis mentioned above. ticker tsla
this is what I tried y= .30 # this is the alpha factor moving_average = float(838.39). this is the first value to be used to calculae ema. for x in df['close']: (x*(1-y)+(y*moving_average)
after running the above loop I get the correct answer for the first value, now what I am looking for is how will I store the new value with the old value and loop again though the data to populate the entire list based on the equation. my out put should look like 1. open 2. high 3. low 4. close 5. volume new_column(ema) date.
1. open 2. high 3. low 4. close 5. volume
date
2022-03-04 849.1000 855.6500 825.1609 838.29 22393287.0
2022-03-03 878.7700 886.4390 832.6001 839.29 20541169.0
2022-03-02 872.1300 886.4800 844.2721 879.89 24881146.0
2022-03-01 869.6800 889.8800 853.7800 864.37 24922287.0
2022-02-28 815.0100 876.8600 814.7075 870.43 33002289.0
2022-02-25 809.2300 819.5000 782.4005 809.87 25355921.0
2022-02-24 700.3900 802.4800 700.0000 800.77 45107425.0
2022-02-23 830.4300 835.2997 760.5600 764.04 31752336.0
2022-02-22 834.1300 856.7338 801.1001 821.53 27387874.0
2022-02-18 886.0000 886.8700 837.6100 856.98 22833947.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|