[英]python array operate on row values of a column
I have a array 我有一个数组
array([[ 1.00000000e+00, 1.08020000e+04, 1.08070000e+04],
[ 1.00000000e+00, 1.08760000e+04, 1.08810000e+04],
[ 1.00000000e+00, 1.09520000e+04, 1.09570000e+04],
[ 1.00000000e+00, 1.18130000e+04, 1.18180000e+04],
[ 1.00000000e+00, 1.21400000e+04, 1.21450000e+04],
[ 1.00000000e+00, 1.26300000e+04, 1.26350000e+04],
[ 1.00000000e+00, 1.50800000e+04, 1.50850000e+04],
[ 1.00000000e+00, 1.56590000e+04, 1.56640000e+04],
[ 1.00000000e+00, 1.70740000e+04, 1.70790000e+04]])
I have to subtract row1 and row2 of column 2 and add the result to a new column in row1. 我必须减去第2列的row1和row2,并将结果添加到row1的新列中。 May someone can help achieving this. 愿有人可以帮助实现这一目标。
result1 =row2-row1
result2 =row3-row2
I want to iterate in a loop for all values in column 2. 我想循环访问第2列中的所有值。
I tried to write this but not able to perform correct slicing: 我尝试编写此代码,但无法执行正确的切片:
testfile = numpy.loadtxt("numpy_test.txt", skiprows=1, usecols=(0,1,2))
i=1
j=i-1
k=1
for array in testfile:
print array[i][k]-array[j][k] #result = arr[1][1] - arr[0][1]
i+=1
j+=1
How to iterate over a loop that is my question, already you helped me in understanding how we may code for elements. 如何遍历循环是我的问题,您已经帮助我了解了如何为元素编码。
i think my answer is not good enough. 我认为我的回答不够好。 However, i cannot comment, so i have to put my thought in the answer. 但是,我无法发表评论,因此我必须将自己的想法放在答案中。
Let your array be a. 让您的数组为a。 My solution is: 我的解决方案是:
for item in a:
item[0] = item[1] - item[0]
# you can add item[1] = item[2] - item[1] if you want
and this will return a with the result. 这将返回结果。
I think you want something like this?: 我想您想要这样的东西?:
array = [[ 1.00000000e+00, 1.08020000e+04, 1.08070000e+04],
[ 1.00000000e+00, 1.08760000e+04, 1.08810000e+04],
[ 1.00000000e+00, 1.09520000e+04, 1.09570000e+04],
[ 1.00000000e+00, 1.18130000e+04, 1.18180000e+04],
[ 1.00000000e+00, 1.21400000e+04, 1.21450000e+04],
[ 1.00000000e+00, 1.26300000e+04, 1.26350000e+04],
[ 1.00000000e+00, 1.50800000e+04, 1.50850000e+04],
[ 1.00000000e+00, 1.56590000e+04, 1.56640000e+04],
[ 1.00000000e+00, 1.70740000e+04, 1.70790000e+04]]
result1 = array[1][1] - array[0][1] # 1.08760000e+04 - 1.08020000e+04
result2 = array[2][1] - array[1][1] # 1.09520000e+04 - 1.08760000e+04
Numpy matrices can be accessed like multi dimensional arrays: 可以像多维数组一样访问Numpy矩阵:
result1 = arr[1][1] - arr[0][1]
result2 = arr[2][1] - arr[1][1]
To add a new column create a new temp array, and after you have copied arr
's contents to it, copy all the contents back to arr
: 要添加新列,请创建一个新的临时数组,然后将arr
的内容复制到其中,然后将所有内容复制回arr
:
temp = np.zeros((arr.shape[0], arr.shape[1]+1))
temp[:,:-1] = arr
temp[0][temp.shape[1]+1] = result1 + result2
All in all: 总而言之:
import numpy as np
arr = np.array([[ 1.00000000e+00, 1.08020000e+04, 1.08070000e+04],
[ 1.00000000e+00, 1.08760000e+04, 1.08810000e+04],
[ 1.00000000e+00, 1.09520000e+04, 1.09570000e+04],
[ 1.00000000e+00, 1.18130000e+04, 1.18180000e+04],
[ 1.00000000e+00, 1.21400000e+04, 1.21450000e+04],
[ 1.00000000e+00, 1.26300000e+04, 1.26350000e+04],
[ 1.00000000e+00, 1.50800000e+04, 1.50850000e+04],
[ 1.00000000e+00, 1.56590000e+04, 1.56640000e+04],
[ 1.00000000e+00, 1.70740000e+04, 1.70790000e+04]])
result1 = arr[1][1] - arr[0][1]
result2 = arr[2][1] - arr[1][1]
temp = np.zeros((arr.shape[0], arr.shape[1]+1))
temp[:,:-1] = arr
temp[0][temp.shape[1]-1] = result1 + result2
print temp
Gives: 得到:
[[ 1.00000000e+00 1.08020000e+04 1.08070000e+04 1.50000000e+02]
[ 1.00000000e+00 1.08760000e+04 1.08810000e+04 0.00000000e+00]
[ 1.00000000e+00 1.09520000e+04 1.09570000e+04 0.00000000e+00]
[ 1.00000000e+00 1.18130000e+04 1.18180000e+04 0.00000000e+00]
[ 1.00000000e+00 1.21400000e+04 1.21450000e+04 0.00000000e+00]
[ 1.00000000e+00 1.26300000e+04 1.26350000e+04 0.00000000e+00]
[ 1.00000000e+00 1.50800000e+04 1.50850000e+04 0.00000000e+00]
[ 1.00000000e+00 1.56590000e+04 1.56640000e+04 0.00000000e+00]
[ 1.00000000e+00 1.70740000e+04 1.70790000e+04 0.00000000e+00]]
This is how I would do it: 这就是我要做的:
import numpy as np
A = np.array([[ 1.0, 1.0802e4, 1.0807e4],
[ 1.0, 1.0876e4, 1.0881e4],
[ 1.0, 1.0952e4, 1.0957e4],
[ 1.0, 1.1813e4, 1.1818e4],
[ 1.0, 1.2140e4, 1.2145e4],
[ 1.0, 1.2630e4, 1.2635e4],
[ 1.0, 1.5080e4, 1.5085e4],
[ 1.0, 1.5659e4, 1.5664e4],
[ 1.0, 1.7074e4, 1.7079e4]])
# First add a column to the array
m, n = A.shape
new_column = np.zeros((m, 1))
A = np.hstack((A, new_column))
# Now loop over the rows and calculate differences
for i in range(m-1):
A[i, 3] = A[i+1, 1] - A[i, 1]
# Or alternatively, without a loop
A[:-1, 3] = np.diff(A[:, 1])
Notice that you can't iterate the Numpy array directly as for array in testfile
because you need values from multiple rows. 请注意,您不能像测试文件for array in testfile
直接迭代Numpy for array in testfile
因为您需要多行中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.