简体   繁体   中英

How to filter and append arrays

I have a code that I am using to try and filter out arrays that are missing values as seen here:

from astropy.table import Table
import numpy as np

data = '/home/myname/data.fits'
data = Table.read(data, format="fits") 

ID = np.array(data['id'])
ID.astype(str)

redshift = np.array(data['z'])
redshift.astype(float)

radius = np.array(data['r']) 
radius.astype(float)

mag = np.array(data['M'])
mag.astype(float)

def stack(array1, array2, array3, array4):
    #stacks multiple arrays to have corresponding values next to eachother
    stacked_array = [(array1[i], array2[i], array3[i], array4[i]) for i in range(0, array1.size)]
    stacked_array = np.array(stacked_array)
    return(stacked_array) 

stacked = stack(ID, redshift, radius, mag)

filtered_array = np.array([])

for i in stacked:
    if not i.any == 'nan':
        np.insert(filtered_array, i[0], axis=0)

The last for loop is where i'm having difficulty. I want to insert the rows from my stacked array into my filtered array if it has all of the information (some rows are missing redshift, others are missing magnitude etc...). How would I be able to loop over my stacked array and filter out all of the rows that have all 4 values I want? I keep getting this error currently.

TypeError: _insert_dispatcher() missing 1 required positional argument: 'values'

So something like this?

a=[[1,2,3,4],[1,"nan",2,3]]
b=[i for i in a if not any(j=='nan' for j in i)]

which prints [[1, 2, 3, 4]] .

You can switch:

for i in stacked:
    if not i.any == 'nan':
        np.insert(filtered_array, i[0], axis=0)

to:

def any_is_nan(col):
    return len(list(filter(lambda x: x=='nan',col))) > 0

filtered_array = list(filter(lambda x: not any_is_nan(x),stacked))

Please refer to filter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM