Python 在给定条件的numpy数组中填充值
目前我有一个数组,如下所示:Python 在给定条件的numpy数组中填充值,python,arrays,numpy,Python,Arrays,Numpy,目前我有一个数组,如下所示: myArray = np.array( [[ 976.77 , 152.95 , 105.62 , 53.44 , 0 ], [ 987.61 , 156.63 , 105.53 , 51.1 , 0 ], [1003.74 , 151.31 , 104.435, 52.86 , 0 ], [ 968. , 153.41 , 106.24 , 58.98 , 0 ],
myArray = np.array(
[[ 976.77 , 152.95 , 105.62 , 53.44 , 0 ],
[ 987.61 , 156.63 , 105.53 , 51.1 , 0 ],
[1003.74 , 151.31 , 104.435, 52.86 , 0 ],
[ 968. , 153.41 , 106.24 , 58.98 , 0 ],
[ 978.66 , 152.19 , 103.28 , 57.97 , 0 ],
[1001.9 , 152.88 , 105.08 , 58.01 , 0 ],
[1024.93 , 146.59 , 107.06 , 59.94 , 0 ],
[1020.01 , 148.05 , 109.96 , 58.67 , 0 ],
[1034.01 , 152.69 , 107.64 , 59.74 , 0 ],
[ 0. , 154.88 , 102. , 58.96 , 0 ],
[ 0. , 147.46 , 100.69 , 54.95 , 0 ],
[ 0. , 149.7 , 102.439, 53.91 , 0 ]]
)
我希望用上一个最后的值(1034.01)填充第一列中的零,但是如果0从索引0开始,则它将保持为0
最终结果示例:
myArrayEnd = np.array(
[[ 976.77 , 152.95 , 105.62 , 53.44 , 0 ],
[ 987.61 , 156.63 , 105.53 , 51.1 , 0 ],
[1003.74 , 151.31 , 104.435, 52.86 , 0 ],
[ 968. , 153.41 , 106.24 , 58.98 , 0 ],
[ 978.66 , 152.19 , 103.28 , 57.97 , 0 ],
[1001.9 , 152.88 , 105.08 , 58.01 , 0 ],
[1024.93 , 146.59 , 107.06 , 59.94 , 0 ],
[1020.01 , 148.05 , 109.96 , 58.67 , 0 ],
[1034.01 , 152.69 , 107.64 , 59.74 , 0 ],
[1034.01 , 154.88 , 102. , 58.96 , 0 ],
[1034.01 , 147.46 , 100.69 , 54.95 , 0 ],
[1034.01 , 149.7 , 102.439, 53.91 , 0 ]]
)
我希望该代码适用于任何阵列,而不仅仅是这一个,在这种情况下可能会有所不同。(列3可能是所有0个,而第4列可能有0个在中间,应该用上一个前一个值填充)。
编辑:与@ukemi结合使用,后者具有更快的解决方案,但不会在各个列上循环。此外,您还需要确保不要尝试为数组[0][1]编制索引。假设我正确理解了您的意思,这应该可以做到:
def fill_zeroes(array):
temp_array = array
for i in xrange(1, len(temp_array)):
if temp_array[i][0] == 0:
temp_array[i][0] = temp_array[i-1][0]
return temp_array
以下代码需要测试:
values = myArray.to_list() # don't remember if nd_array.to_list is a method or property
result = []
last = None
for i,item in enumerate(values):
if i == 0 and item[0] == 0:
last = item
elif item[0] == 0 and last is not None:
item[0] = last
else:
last = item[0]
result.append(item)
这是一种使用
熊猫的矢量化方法。这也可以通过numpy
实现。在任何情况下,此任务都不需要显式循环
import pandas as pd
import numpy as np
df = pd.DataFrame(myArray)\
.replace(0, np.nan)\
.ffill().fillna(0)
res = df.values
print(res)
[[ 976.77 152.95 105.62 53.44 0. ]
[ 987.61 156.63 105.53 51.1 0. ]
[ 1003.74 151.31 104.435 52.86 0. ]
[ 968. 153.41 106.24 58.98 0. ]
[ 978.66 152.19 103.28 57.97 0. ]
[ 1001.9 152.88 105.08 58.01 0. ]
[ 1024.93 146.59 107.06 59.94 0. ]
[ 1020.01 148.05 109.96 58.67 0. ]
[ 1034.01 152.69 107.64 59.74 0. ]
[ 1034.01 154.88 102. 58.96 0. ]
[ 1034.01 147.46 100.69 54.95 0. ]
[ 1034.01 149.7 102.439 53.91 0. ]]
停留在numpy
:
for k, c in enumerate(myArray.T):
idx = np.flatnonzero(c == 0)
if idx.size > 0 and idx[0] > 0:
myArray[idx, k] = myArray[idx[0] - 1, k]
将只存在于一个列的末尾,或者这个0可以存在于一个列的中间?一个列是否会出现类似[1,0,1]的情况?如果是这样,那么结果应该是[1,1,1]还是仅仅是[1,0,1]?@Tai是的,在我的实际代码中会存在类似[1,0,1]的数组,[1,0,1]列的理想输出应该是什么?如果你想坚持使用numpy
,这是一个不错的解决方案。值得一提的是,可以使用numba
优化这些循环操作。
for k, c in enumerate(myArray.T):
idx = np.flatnonzero(c == 0)
if idx.size > 0 and idx[0] > 0:
myArray[idx, k] = myArray[idx[0] - 1, k]