title: How to remove values from a Python array, perform an operation on them and then replace them in the original array
body: <p>I'm working with a huge dataset. What I want to do is take all values > 0 from the array and place them in a new array, run statistics on those extracted values and then place the new values back in the original array.  </p>
  
  <p>Suppose I have an array <code>[0,0,0,0,0, . . . .32, .44,0,0,0]</code> (i.e. the object <code>arr</code> in the script below): I want to remove the values such as .32, .44, etc., and put them in a new array <code>arr2</code>.</p>
  
  <p>Then I want to do a statistical analysis (PCA) on this second array, take the new values corresponding with the original position in the original array and replace the original values with these new values. I've started coding this below, but have no idea how to extract values > 0 while maintaining the position in the array.</p>
  
  <pre><code>import os
  import nibabel as nb
  import numpy as np
  import numpy.linalg as npl
  import nibabel as nib
  import matplotlib.pyplot as plt
  from matplotlib.mlab import PCA
  #from dipy.io.image import load_nifti, save_nifti
  
  np.set_printoptions(precision=4, suppress=True)
  FA = './all_FA_skeletonised.nii'
  
  from dipy.io.image import load_nifti
  img = nib.load(FA)
  data = img.get_data()
  data.shape        #get x,y,z and subject # parameters from image
  
  #place subject number into a variable
  vol_shape = data.shape[:-1] # x,y,z coordinates
  n_vols = data.shape[-1]   # 28 subjects volumes
  
  # N is the num of voxels (dimensions) in a volume
  N = np.prod(vol_shape)
  
  #- Reshape first dimension of whole image data array to N, and take
  #- transpose
  arr2 = []
  arr = data.reshape(N, n_vols).T  # 28 X 7,200,000 array
  for a in array:
      if a &gt; 0:
          arr2.append(a)
  
  row_means = np.outer(np.mean(arr2, axis=1), np.ones(N))
  X = arr2 - row_means # mean center data array
  
  #- Calculate unscaled covariance matrix of X
  unscaled_covariance = X.dot(X.T)
  unscaled_covariance.shape
  
  # Calculate U, S, VT with SVD on unscaled covariance matrix
  U, S, VT = npl.svd(unscaled_covariance)
  #- Use subplots to make axes to plot first 10 principal component
  #- vectors
  #- Plot one component vector per sub-plot.
  fig, axes = plt.subplots(10, 1)
  for i, ax in enumerate(axes):
      ax.plot(U[:, i])
  
  #- Calculate scalar projections for projecting X onto U
  #- Put results into array C.
  C = U.T.dot(X)
  
  ***#- Put values in C back into original data matrix***
  </code></pre>