Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. df = pd.DataFrame()
  4.  
  5. df['A'] = (10,20,34,13,45,2,34,1,18,19,23,9,40,33,17,6,15)
  6.  
  7. df['B'] = (14,26,23,41,12,24,31,1,9,53,4,22,16,19,16,28,13)
  8.  
  9. print(df)
  10.  
  11. # shift up rows
  12. s = df[['A', 'B']].shift(-5)
  13.  
  14. # compare against "A" and mask NaNs
  15. m = s.lt(df['A'], axis=0).mask(s.isna())
  16.  
  17. # create and concatenate the result
  18. df2 = pd.DataFrame(
  19. np.select([m == 1, m == 0], ['TRUE', 'FALSE'], default='IGNORE'),
  20. columns=['C', 'D'],
  21. index=df.index)
  22. pd.concat([df, df2], axis=1)
  23.  
  24. A B C D
  25. 0 10 14 TRUE FALSE
  26. 1 20 26 FALSE FALSE
  27. 2 34 23 TRUE TRUE
  28. 3 13 41 FALSE TRUE
  29. 4 45 12 TRUE FALSE
  30. 5 2 24 FALSE FALSE
  31. 6 34 31 TRUE TRUE
  32. 7 1 1 FALSE FALSE
  33. 8 18 9 FALSE FALSE
  34. 9 19 53 TRUE TRUE
  35. 10 23 4 TRUE FALSE
  36. 11 9 22 FALSE FALSE
  37. 12 40 16 IGNORE IGNORE
  38. 13 33 19 IGNORE IGNORE
  39. 14 17 16 IGNORE IGNORE
  40. 15 6 28 IGNORE IGNORE
  41. 16 15 13 IGNORE IGNORE
  42.  
  43. s=np.where(df.A.shift(-5).isna(),'ignore',df.A>df.A.shift(-5))
  44. s
  45. Out[90]:
  46. array(['True', 'False', 'True', 'False', 'True', 'False', 'True', 'False',
  47. 'False', 'True', 'True', 'False', 'ignore', 'ignore', 'ignore',
  48. 'ignore', 'ignore'], dtype='<U6'
  49.  
  50. t=np.where(df.B.shift(-5).isna(),'ignore',df.A>df.B.shift(-5))
  51.  
  52. df['col1'],df['col2']=s,t
  53.  
  54. s=df.shift(-5).ge(df.A,0).mask(df.A.shift(-5).isna(),'ignore')
  55. s.columns=['col1','col2']
  56. df=pd.concat([df,s],axis=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement