Python 熊猫换行
我有一个数据框中的矩阵Python 熊猫换行,python,pandas,matrix,dataframe,min,Python,Pandas,Matrix,Dataframe,Min,我有一个数据框中的矩阵 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
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
因此它会更改列中的值,而不是行中的值,
如何为行实现它?
thx您可以用每行的最小值减去:
我还尝试重写您的函数-我添加用于选择:
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
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