SHARE
TWEET

pandas - work with rows in subset

furas Jul 19th, 2018 (edited) 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2.  
  3. data = pd.DataFrame({
  4.     'estA': [1., 2., 3., 4.],
  5.     'estB': [5., 6., 7., 8.],
  6.     'estC': [8., 7., 6., 5.],
  7.     'estZ': [0., 3., 0., 1.],    
  8. })
  9.  
  10. #print(data)
  11.  
  12. Am = data['estA'].mean()
  13. Bm = data['estB'].mean()
  14. Cm = data['estC'].mean()
  15. Zm = data['estZ'].mean()
  16.  
  17. #print(Am, Bm, Cm, Zm)
  18.  
  19. for index, row in data.iterrows():
  20.     if row['estZ'] == 0:
  21.         row['estZ'] = Zm/len(data) * (Am/row['estA'] + Bm/row['estB'] + Cm/row['estC'])
  22.  
  23. print(data)
  24.  
  25. #-------------------------------------
  26.  
  27. data = pd.DataFrame({
  28.     'estA': [1., 2., 3., 4.],
  29.     'estB': [5., 6., 7., 8.],
  30.     'estC': [8., 7., 6., 5.],
  31.     'estZ': [0., 3., 0., 1.],    
  32. })
  33.  
  34. #print(data)
  35.  
  36. Am = data['estA'].mean()
  37. Bm = data['estB'].mean()
  38. Cm = data['estC'].mean()
  39. Zm = data['estZ'].mean()
  40.  
  41. #print(Am, Bm, Cm, Zm)
  42.  
  43. subset = data[ data['estZ'] == 0 ]
  44.  
  45. for index, row in subset.iterrows():
  46.     data['estZ'][index] = Zm/len(data) * (Am/row['estA'] + Bm/row['estB'] + Cm/row['estC'])
  47.  
  48. print(data)
  49.  
  50. #-------------------------------------
  51.  
  52. data = pd.DataFrame({
  53.     'estA': [1., 2., 3., 4.],
  54.     'estB': [5., 6., 7., 8.],
  55.     'estC': [8., 7., 6., 5.],
  56.     'estZ': [0., 3., 0., 1.],    
  57. })
  58.  
  59. #print(data)
  60.  
  61. Am = data['estA'].mean()
  62. Bm = data['estB'].mean()
  63. Cm = data['estC'].mean()
  64. Zm = data['estZ'].mean()
  65.  
  66. data['estZ'] = data.apply(lambda row: row['estZ'] if row['estZ'] != 0 else Zm/len(data) * (Am/row['estA'] + Bm/row['estB'] + Cm/row['estC']), axis=1)
  67.  
  68. print(data)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top