<p>既然您导入了<code>numpy</code>,您就可以像<code>pandas</code>一样轻松地使用它:</p>
<p>从样本的粘贴副本中读取:</p>
<pre><code>In [36]: txt="""date,high,low,precip
...: 1-Jan,43,41,0
...: 2-Jan,50,25,0
...: 3-Jan,51,25,0
...: 4-Jan,44,25,0
...: 5-Jan,36,21,0
...: 6-Jan,39,20,0
...: 7-Jan,47,21,0.04
...: 8-Jan,30,14,0
...: 9-Jan,30,12,0"""
</code></pre>
<p>numpy 1.14的Python3喜欢使用<code>encoding</code>参数:</p>
<pre><code>In [38]: data = np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None,names=True,
...: encoding=None)
In [39]: data
Out[39]:
array([('1-Jan', 43, 41, 0. ), ('2-Jan', 50, 25, 0. ),
('3-Jan', 51, 25, 0. ), ('4-Jan', 44, 25, 0. ),
('5-Jan', 36, 21, 0. ), ('6-Jan', 39, 20, 0. ),
('7-Jan', 47, 21, 0.04), ('8-Jan', 30, 14, 0. ),
('9-Jan', 30, 12, 0. )],
dtype=[('date', '<U5'), ('high', '<i8'), ('low', '<i8'), ('precip', '<f8')])
</code></pre>
<p>结果是一个结构化数组,从中很容易选择<code>high</code>字段:</p>
<pre><code>In [40]: data['high']
Out[40]: array([43, 50, 51, 44, 36, 39, 47, 30, 30])
In [41]: data['high'].mean()
Out[41]: 41.111111111111114
</code></pre>
<p>或者在一行中,只加载一列:</p>
<pre><code>In [44]: np.genfromtxt(txt.splitlines(),delimiter=',',skip_header=1,usecols=[1]).mean()
Out[44]: 41.111111111111114
</code></pre>