我编写了一个脚本,其中从文件的列创建列表。在此列表中,有许多随机放置的 nan 条目。我怎样才能删除这些条目?这是我的代码:
#import astropy.io.ascii as asciitable
import numpy as np
import pylab as plt
#x=asciitable.read('protected.txt', guess=False,delimiter='\t',fill_values=[('', '-999')])
#x=np.genfromtxt('protected.txt', comments='#', delimiter=' ', skiprows=0, skip_header=0, skip_footer=0, converters=None, missing='', missing_values='', filling_values=-999, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True)
x=np.load('EC/EC_data')
# Convert columns to float values
BMI=map(float,x['bmiEC'])
print BMI
这是追溯:
TypeError Traceback (most recent call last)
/example_bmiEC.py in <module>()
8 # Convert columns to float values
9 BMI=map(float,x['bmiEC'])
---> 10 BMI=BMI[~np.isnan(BMI)]
11 print BMI
TypeError: only integer arrays with one element can be converted to an index
WARNING: Failure executing file: <example_bmiECFSPR.py>
>>> import numpy as np
>>> a = np.array([1, 2, 3., np.nan, 4, np.nan])
>>> a = a[~np.isnan(a)]
>>> a
array([ 1., 2., 3., 4.])
我是一名优秀的程序员,十分优秀!