如何从AstroPy表中删除空白元素?

时间:2016-08-24 12:05:07

标签: python pandas python-3.5 astropy

我试图从具有空白字段的astropy表中删除任何元素。但是,到目前为止我找到的所有帮助只是告诉我如何更换它。我尝试用零替换这些空白字段(用' - '表示)。但是,每当我尝试使用传统的python循环过滤它们时,它们就会保持不变。 我无法删除其中包含空白字段的行吗?

from astropy.io import ascii

dat = ascii.read('exorgdatabase.txt')
dat['MSINI'].fill_value = 0
print(dat['MSINI'])
dat['PER'].fill_value = 0
print(dat['PER'])
newdat = dat.filled()
print(newdat)


while '0' in newdat['MSINI']:
    newdat.remove('0')

print(newdat['MSINI'])

这是我得到的输出:

 MSINI  
--------
mjupiter
      --
      --
      --
0.310432
      --
      --
      --
 7.65457
      --
     ...
      --
      --
      --
      --
      --
      --
      --
      --
      --
      --
      --
Length = 5455 rows
    PER    
-----------
        day
   7.958203
 3.27346074
19.12947337
  10.290994
  27.495606
   9.478522
 5.03728015
   2.243752
  7.8125124
        ...
   7.227407
  91.069934
 366.084069
  414.45008
 5.43099314
  328.32211
  381.97977
  67.412998
 2.08802799
359.8249913
  293.70696
Length = 5455 rows
     NAME      MSINI   ...                      FIRSTURL                    
------------- -------- ... -------------------------------------------------
          N/A mjupiter ...                                               N/A
 Kepler-107 d        0 ... http://adsabs.harvard.edu/abs/2014arXiv1402.6534R
Kepler-1049 b        0 ... http://adsabs.harvard.edu/abs/2016ApJ...822...86M
 Kepler-813 b        0 ... http://adsabs.harvard.edu/abs/2016ApJ...822...86M
 Kepler-427 b 0.310432 ... http://adsabs.harvard.edu/abs/2010Sci...327..977B
Kepler-1056 b        0 ... http://adsabs.harvard.edu/abs/2016ApJ...822...86M
Kepler-1165 b        0 ... http://adsabs.harvard.edu/abs/2016ApJ...822...86M
Kepler-1104 b        0 ... http://adsabs.harvard.edu/abs/2016ApJ...822...86M
    WASP-14 b  7.65457 ... http://adsabs.harvard.edu/abs/2009MNRAS.392.1532J
  Kepler-50 b        0 ... http://adsabs.harvard.edu/abs/2011ApJ...736...19B
          ...      ... ...                                               ...
  KOI 2369.03        0 ...                                               N/A
  KOI 7572.01        0 ...                                               N/A
  KOI 7587.01        0 ...                                               N/A
  KOI 7589.01        0 ...                                               N/A
  KOI 2859.05        0 ...                                               N/A
  KOI 7591.01        0 ...                                               N/A
  KOI 7592.01        0 ...                                               N/A
  KOI 7593.01        0 ...                                               N/A
  KOI 7594.01        0 ...                                               N/A
  KOI 7596.01        0 ...                                               N/A
  KOI 7599.01        0 ...                                                 e
Length = 5455 rows
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-118-c200fae23235> in <module>()
     23 
     24 while '0' in newdat['MSINI']:
---> 25     newdat.remove('0')
     26 
     27 print(newdat['MSINI'])

AttributeError: 'Table' object has no attribute 'remove'

1 个答案:

答案 0 :(得分:1)

你不能!

async删除单个字段是不可能的,因为它基于astropy.table.Table。因此,您只能删除整个列或行,而不能删除单个元素。

例如,你有这样一个表:

numpy

您可以用

替换蒙版值
>>> from astropy.table import Table, MaskedColumn, Column

>>> a = MaskedColumn([1, 2], name='a', mask=[False, True], dtype='i4')
>>> b = Column([3, 4], name='b', dtype='i8')
>>> tbl = Table([a, b])
>>> tbl
 a   b 
--- ---
  1   3
 --   4

例如,删除>>> tbl = tbl.filled(0) >>> tbl a b --- --- 1 3 0 4 为0的所有行:

a

或通过访问>>> tbl[tbl['a'] != 0] a b --- --- 1 3

填写它
mask