【发布时间】:2019-08-06 01:41:01
【问题描述】:
我正在尝试使用Numpy 读取text file 中的数据,这些数据如下所示:
# Student data collected on 17 July 2014
# Researcher: Dr Wicks, University College Newbury
# The following data relate to N = 20 students. It
# has been totally made up and so therefore is 100%
# anonymous.
Subject Sex DOB Height Weight BP VO2max
(ID) M/F dd/mm/yy m kg mmHg mL.kg-1.min-1
JW-1 M 19/12/95 1.82 92.4 119/76 39.3
JW-2 M 11/1/96 1.77 80.9 114/73 35.5
JW-3 F 2/10/95 1.68 69.7 124/79 29.1
JW-6 M 6/7/95 1.72 75.5 110/60 45.5
# JW-7 F 28/3/96 1.66 72.4 101/68 -
JW-9 F 11/12/95 1.78 82.1 115/75 32.3
JW-10 F 7/4/96 1.60 - -/- 30.1
JW-11 M 22/8/95 1.72 77.2 97/63 48.8
JW-12 M 23/5/96 1.83 88.9 105/70 37.7
JW-14 F 12/1/96 1.56 56.3 108/72 26.0
JW-15 F 1/6/96 1.64 65.0 99/67 35.7
JW-16 M 10/9/95 1.63 73.0 131/84 29.9
JW-17 M 17/2/96 1.67 89.8 101/76 40.2
JW-18 M 31/7/96 1.66 75.1 -/- -
JW-19 F 30/10/95 1.59 67.3 103/69 33.5
JW-22 F 9/3/96 1.70 - 119/80 30.9
JW-23 M 15/5/95 1.97 89.2 124/82 -
JW-24 F 1/12/95 1.66 63.8 100/78 -
JW-25 F 25/10/95 1.63 64.4 -/- 28.0
JW-26 M 17/4/96 1.69 - 121/82 39.
我阅读了sex 和Height columns 并且在下面的代码中我没有遇到任何问题:
import numpy as np
fname = 'D:\\NumpyTutorial.txt'
datatype1 = np.dtype([('sex','|S1'),('height','f8')])
a = np.loadtxt(fname, dtype=datatype1, skiprows=9, usecols=(1,3))
print(a)
但是当我尝试使用以下代码阅读Weight column 时:
import numpy as np
fname = 'D:\\NumpyTutorial.txt'
datatype1 = np.dtype([('sex','|S1'),('height','f8'),('Weight','f8')])
a = np.loadtxt(fname, dtype=datatype1, skiprows=9, usecols=(1,3,4))
print(a)
我收到了这个错误:
ValueError: could not convert string to float: '-'.
1- 我该如何处理这个sign(-)。我该如何阅读这个专栏?
2- 对于sex column,我使用'|S1',对于height,使用'f8',Subject, BOD and BP columns 的syntax 是什么,直到阅读它们?
3 - 如何显示此文件中的所有内容?
【问题讨论】:
-
genfromtxt更好地处理缺失值 -
我会去看看@hpaulj
-
@hpaulj 它给了我一个完整的列表 (b'J', nan)
-
有什么理由不想使用 Pandas?
-
@BallpointBen 亲爱的先生,我正在学习。我还没有带熊猫。