How Do I Subtract The Previous Row From The Current Row In A Pandas Dataframe And Apply It To Every Row; Without Using A Loop?
I am using Python3.5 and I am working with pandas. I have loaded stock data from yahoo finance and have saved the files to csv. My DataFrames load this data from the csv. This is
Solution 1:
you can use pct_change() or/and diff() methods
Demo:
In[138]: df.Close.pct_change() * 100Out[138]:
0NaN10.46948420.4672903-0.93023340.46948450.46729060.0000007-3.2558148-3.3653859-0.497512Name: Close, dtype: float64In[139]: df.Close.diff()
Out[139]:
0NaN10.12520.1253-0.25040.12550.12560.0007-0.8758-0.8759-0.125Name: Close, dtype: float64
Solution 2:
MaxU solutions suits in your case. If you want to perform more complex computations based on your previous rows you should use shift
Post a Comment for "How Do I Subtract The Previous Row From The Current Row In A Pandas Dataframe And Apply It To Every Row; Without Using A Loop?"