rh1990
rh1990

Reputation: 940

Deleting masked elements in arrays by index

I have three arrays and one contains masked values based off some condition:

a = numpy.ma.MaskedArray(other_array, condition)

(Initially I used masked arrays because my condition is a variable and it made plotting a lot easier with other data sets to keep my arrays fixed lengths. Now I'm exporting my data to be analysed by other program not written by me, and it can't handle '--')

So my arrays have the form:

a = [1,--,3]
b = [4,5,6]
c = [7,8,9]

I want to iterate through a, identify any index of a that contains a masked value '--', and then delete that index from all arrays:

a = [1,3]
b = [4,6]
c = [7,9]

In reality, a b and c are very long, and the masked indices aren't regularly spaced.

Thanks!

Upvotes: 0

Views: 725

Answers (2)

jafarbtech
jafarbtech

Reputation: 7015

Try cloning the array and pop on it and replace the temp array like

a = [1,'--',3]
b = [4,5,6]
c = [7,8,9]
t=a[:]
for i in range(len(a)):
    try:
        value = int(a[i])
    except ValueError:
        t.pop(i)
        b.pop(i)
        c.pop(i)
a=t
print a
print b
print c

You can also use equal when you have the same symbol for masked value

a = [1,'--',3]
b = [4,5,6]
c = [7,8,9]
t=a[:]
for i in range(len(a)):
    if a[i]=='--':
        t.pop(i)
        b.pop(i)
        c.pop(i)
a=t
print a
print b
print c

Upvotes: 0

Dinesh Suthar
Dinesh Suthar

Reputation: 148

If there are only 3 lists, you can use pop() function to delete the indexes from List B & C. Pass the Index to pop() where it is '--' in List A.

for i in range(len(a)):
    if numpy.ma.is_masked(a[i]):
        b.pop(i)
        c.pop(i)

It will delete that Index from the lists B & C, where '--' is present in List A.

Upvotes: 1

Related Questions