I have the following DataFrame :
dates = pd.date_range('20150101', periods=4) df = pd.DataFrame({'A' : [5,10,3,4]}, index = dates) df.loc[:,'B'] = 0 df.loc[:,'C'] = 0 df.iloc[0,1] = 10 df.iloc[0,2] = 3 print df Out[69]: ABC 2015-01-01 5 10 3 2015-01-02 10 0 0 2015-01-03 3 0 0 2015-01-04 4 0 0
I want to implement the following logic for columns B and C :
B(k+1) = B(k) - A(k+1)C(k+1) = B(k) + A(k+1)
I can do this using the following code:
for i in range (1, df.shape[0]): df.iloc[i,1] = df.iloc[i-1,1] - df.iloc[i,0] df.iloc[i,2] = df.iloc[i-1,1] + df.iloc[i,0] print df
This gives:
ABC 2015-01-01 5 10 3 2015-01-02 10 0 20 2015-01-03 3 -3 3 2015-01-04 4 -7 1
What kind of answer am I looking for. The problem is that I apply this to a DataFrame when a large data array is slow. So slow. Is there a better way to achieve this?
python pandas dataframe
Anthony w
source share