gpt4 book ai didi

python - 如何从我的 python 代码中删除 nans

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:09 24 4
gpt4 key购买 nike

我编写了一个脚本,其中从文件的列创建列表。在此列表中,有许多随机放置的 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.])

关于python - 如何从我的 python 代码中删除 nans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18360641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com