python - change rows in pandas -


i have matrix in pandas data frame

print dfmatrix        0       1      2      3       4 0  10000      10      8     11      10 1     10  100000     13      9      10 2      8      13  10000      9      11 3     11       9      9  10000      12 4     10      10     11     12  100000 

i need change row values reducing each row value minimum row(row row) here code try:

def matrixreduction(matrix):     minrowvalues = matrix.min(axis=1)     in xrange(matrix.shape[1]):         matrix[i][:] = matrix[i][:] - minrowvalues[i]     return matrix 

and expect output like:

      0     1     2     3     4  0 9992     2     0     3     2  1    1 99991     4     0     1  2    0     5  9992     1     3  3    2     0     0  9991     3  4    0     0     1     2 99990 

but such output:

      0      1     2     3      4 0  9992      1     0     2      0 1     2  99991     5     0      0 2     0      4  9992     0      1 3     3      0     1  9991      2 4     2      1     3     3  99990 

so changes values in columns instead of rows, how achieve rows? thx

you can subtract sub minimal values per rows min:

print (df.min(axis=1)) 0     8 1     9 2     8 3     9 4    10 dtype: int64  print (df.sub(df.min(axis=1), axis=0))       0      1     2     3      4 0  9992      2     0     3      2 1     1  99991     4     0      1 2     0      5  9992     1      3 3     2      0     0  9991      3 4     0      0     1     2  99990 

i try rewrite function - add ix selecting:

def matrixreduction(matrix):     minrowvalues = matrix.min(axis=1)     in range(matrix.shape[1]):         matrix.ix[i,:] = matrix.ix[i, :] - minrowvalues[i]     return matrix 

timings:

in [136]: %timeit (matrixreduction(df)) 100 loops, best of 3: 2.64 ms per loop  in [137]: %timeit (df.sub(df.min(axis=1), axis=0)) slowest run took 5.49 times longer fastest. mean intermediate result being cached. 1000 loops, best of 3: 308 µs per loop 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -