Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill values in a numpy array given a condition

Currently I have an array as follows:

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 ]]
)

I would like the fill in the zeros in the first column with the previous last value (1034.01) however if the 0's start from index 0, for it to remain as 0.

Example of end result:

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 ]]
)

I would like the code to be applicable to any array not just this one, where the situation may be different. (Column 3 might be all 0's and Column 4 might have 0's in the middle which should be filled with the last previous value).

like image 694
lee.edward01 Avatar asked Jan 27 '26 20:01

lee.edward01


1 Answers

Here's a vectorised way with pandas. This is also possible with numpy. In any case, you should not need explicit loops for this task.

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.   ]]
like image 193
jpp Avatar answered Jan 30 '26 09:01

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!