如何删除astropy Table中包含掩码元素的行?
Remove Rows with Masked Elements Using Native Astropy
Perfect, you don't need Pandas for this—Astropy has all the tools you need built right in. Here's how to filter out any rows that have at least one masked (missing) value:
from astropy.io import ascii weather_data = """ day,precip,type ,1.5,rain Tues, 9., Wed,1.1,snow ed,,aaa Wd,1.1,snow """ dat = ascii.read(weather_data) # Filter to keep only rows with NO masked values clean_table = dat[~dat.mask.any(axis=1)]
After running this, clean_table will only contain the Wed and Wd rows you want.
Breakdown of the logic:
dat.maskgives you a boolean array that lines up with your table—eachTrueindicates a masked (missing) value in that position..any(axis=1)scans each row and returnsTrueif there's any masked value in that row (so this is the set of rows we want to exclude).- Using
~to invert that boolean array gives us the rows with no masked values, and slicing the original table with this array keeps only those valid rows.
内容的提问来源于stack exchange,提问作者Gabriel
相关产品推荐
相关产品推荐

