更改 pandas 中的行

change rows in pandas

我在 pandas 数据框中有一个矩阵

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

我需要通过从该行(逐行)中减少每行值的最小值来更改行值 这是我尝试的代码:

def matrixReduction(matrix):
    minRowValues = matrix.min(axis=1)
    for i in xrange(matrix.shape[1]):
        matrix[i][:] = matrix[i][:] - minRowValues[i]
    return matrix

并期望输出如下:

      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

但我得到这样的输出:

      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

所以它改变的是列中的值而不是行中的值, 我如何为行实现它? 谢谢

可以减去sub minimal values per rows by 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

我也尝试重写你的函数 - 我添加 ix 用于选择:

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

时间:

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))
The slowest run took 5.49 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 308 µs per loop