Astropy:删除包含nan的行



我正在使用astopy来操作FITS表,我想删除所有包含nan的行。

使用mytable中存储的拟合表,我尝试了以下

data = np.lib.recfunctions.structured_to_unstructured(np.array(mytable))
idx = []
for i, line in enumerate(data):
for e in line:
if e !=e:
idx.append(i)
data = Table([data[i] for i in range(len(data)) if i not in idx])

这似乎有效,但相当笨重。有没有更像蟒蛇的方法?


如果该行中的一个元素是nan,我想删除整行。这个问题不同,因为它是关于删除单个元素的。

实现这一点的一种更为复杂的方法是:

data = np.lib.recfunctions.structured_to_unstructured(np.array(mytable))
has_nan = np.any(np.isnan(data), axis=1)
mytable_no_nan = mytable[~has_nan]

对于更通用的天体蟒蛇方式(假设mytable是天体Table(:

has_nan = np.zeros(len(mytable), dtype=bool)
for col in mytable.itercols():
if col.info.dtype.kind == 'f':
has_nan |= np.isnan(col)
mytable_no_nan = mytable[~has_nan]

最新更新