Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. In [11]: df
  2. Out[11]:
  3. A B
  4. 2000-01-01 -0.182994 0
  5. 2000-01-02 1.290203 0
  6. 2000-01-03 0.245229 0
  7. 2000-01-08 -1.230742 0
  8. 2000-01-09 0.534939 0
  9. 2000-01-10 1.324027 0
  10.  
  11. for idx,row in df.iterrows():
  12. if df["A"][idx]<-1:
  13. df["B"][idx] = -1
  14. elif df["A"][idx]>1:
  15. df["B"][idx] = 1
  16. else:
  17. df["B"][idx] = df["B"][idx-1]
  18.  
  19. In [38]: df = DataFrame(randn(10,2),columns=list('AB'))
  20.  
  21. In [39]: df['B'] = np.nan
  22.  
  23. In [40]: df.loc[df.A<-1,'B'] = -1
  24.  
  25. In [41]: df.loc[df.A>1,'B'] = 1
  26.  
  27. In [42]: df.ffill()
  28. Out[42]:
  29. A B
  30. 0 -1.186808 -1
  31. 1 -0.095587 -1
  32. 2 -1.921372 -1
  33. 3 -0.772836 -1
  34. 4 0.016883 -1
  35. 5 0.350778 -1
  36. 6 0.165055 -1
  37. 7 1.101561 1
  38. 8 -0.346786 1
  39. 9 -0.186263 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement