Home > Back-end >  How to count the number of days a stock price is higher than the other
How to count the number of days a stock price is higher than the other

Time:04-01

I would Like to get a count of the number of days for which (VZ) stock return is larger than the stock index return (INX)

I already calculated the return using the pct_change function. Now I want to get the number of days VZ is larger than INX. I don't know how to go about this.

Dates          VZ          INX  
2016-03-21    NaN          NaN
2016-03-22  -0.004304   -0.000877
2016-03-23  -0.005638   -0.006386
2016-03-24  0.012285    -0.000378
2016-03-28  -0.002987   0.000545
2016-03-29  0.012172    0.008817
2016-03-30  -0.000185   0.004350
2016-03-31  0.000740    -0.002040
2016-04-01  -0.001294   0.00633

1

CodePudding user response:

To do this you can check which times the "VZ" value is higher than the "INX" value using a column operation, and get the sum of the output:

temp = df["VZ"] > df["INX"]
print(temp.sum())
  • Related