Python 从astropy表中删除行

Python 从astropy表中删除行,python,row,astropy,Python,Row,Astropy,我想从astropy表中删除包含INF的行。类似于下面的内容 for line in mytable: if float('inf') in line: mytable.remove(line) 除了我不知道如何使用remove函数之外 在中,它说明了如何删除列,而不是如何删除行 执行以下操作似乎有效 for col in mytable.colnames: mytable = mytable[mytable[col] != float('inf')] 这比以

我想从astropy表中删除包含INF的行。类似于下面的内容

for line in mytable:
    if float('inf') in line:
        mytable.remove(line)
除了我不知道如何使用
remove
函数之外


在中,它说明了如何删除列,而不是如何删除行

执行以下操作似乎有效

for col in mytable.colnames:
    mytable = mytable[mytable[col] != float('inf')]

这比以前快了一点,尤其是当表的大小增加时

在这里,我们通过将每列掩码组合在一起,为包含
inf
的所有行制作一个掩码,而不是仅对整个表切片一次:

>>> table = Table({'a': [1, 2, 3], 'b': [1.0, np.inf, 3.0], 'c': [np.inf, 2.0, 3.0]})
>>> mask = np.logical_or.reduce([c == np.inf for c in table.columns.values()])
>>> table = table[~mask]
>>> table
<Table length=1>
  a      b       c
int64 float64 float64
----- ------- -------
    3     3.0     3.0
我怀疑对于更多的列和/或行来说,差异更为显著

取决于您的用例是什么,您也可以考虑创建一个带每个列的掩码。这允许您避免从表中删除数据,同时仍对其执行忽略奇异值的算术运算:

>>> table = Table({'a': [1, 2, 3], 'b': [1.0, np.inf, 3.0], 'c': [np.inf, 2.0, 3.0]}, masked=True)
>>> for col in table.columns.values():
...     col.mask = (col == np.inf)
...
>>> table
<Table masked=True length=3>
  a      b       c
int64 float64 float64
----- ------- -------
    1     1.0      --
    2      --     2.0
    3     3.0     3.0
>>> table['b'].mean()
2.0
>>table=table({'a':[1,2,3],'b':[1.0,np.inf,3.0],'c':[np.inf,2.0,3.0]},masked=True)
>>>对于表.columns.values()中的列:
...     col.mask=(col==np.inf)
...
>>>桌子
a、b、c
int64浮点64浮点64浮点64
----- ------- -------
1     1.0      --
2      --     2.0
3     3.0     3.0
>>>表['b'].平均值()
2

mytable.drop(line)
可以删除吗?@CC7052
'Table'对象没有属性'drop'
mytable.remove\u行(0)
它可以工作。在部分
中删除行
>>> table = Table({'a': [1, 2, 3], 'b': [1.0, np.inf, 3.0], 'c': [np.inf, 2.0, 3.0]}, masked=True)
>>> for col in table.columns.values():
...     col.mask = (col == np.inf)
...
>>> table
<Table masked=True length=3>
  a      b       c
int64 float64 float64
----- ------- -------
    1     1.0      --
    2      --     2.0
    3     3.0     3.0
>>> table['b'].mean()
2.0