使用genfromtxt分割数据 [英] using genfromtxt to split data

查看:57
本文介绍了使用genfromtxt分割数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python,我正在使用genfromtxt(来自numpy)将文本文件读入数组:

With Python, I am using genfromtxt (from numpy) to read in a text file into an array:

y = np.genfromtxt("1400list.txt", dtype=[('mystring','S20'),('myfloat','float')])

这行得通,但是似乎没有将我的2列读入2D数组中.我得到了:

Which works okay, except it doesn't seem to read my 2 columns into a 2D array. I am getting:

[('string001', 123.0),('string002', 456.0),('string002', 789.0)]

但是我认为想:

[['string001', 123.0],['string002', 456.0],['string002', 789.0]]

我基本上希望每条信息都是一个单独的元素,然后可以对其进行操作.

I basically want each piece of information as a separate element that I can then manipulate.

推荐答案

genfromtxt返回的内容称为结构化数组.它给出一个一维元组数组,每个元组都具有您指定的dtype.

What genfromtxt returns is called a structured array. It gives a 1d array of tuples, each tuple has the dtype that you specified.

一旦您学会了如何使用它们,它们实际上将非常有用.您不能使用带有浮点数和字符串的2d数组,但是可以使用结构化数组!

These are actually very useful once you learn how to use them. You cannot have a 2d array with floats and strings, but with a structured array, you can!

例如:

import numpy as np
from StringIO import StringIO
s = """string001 123
       string002 456
       string002 789"""
f = StringIO(s)
y = np.genfromtxt(f, dtype=[('mystring', 'S20'), ('myfloat', float)])

到目前为止,您所拥有的是什么.现在,您可以按照以下方式访问y.您可以使用字段名称将一列作为一维数组:

Which is what you have so far. Now you can access y in the following fashion. You can use a field name to get a column as a 1d array:

>>> y['mystring']
array(['string001', 'string002', 'string002'], 
  dtype='|S20')

>>> y['myfloat']
array([ 123.,  456.,  789.])

请注意,由于dtype参数,y['myfloat']给出了float,即使在文件中它们是int.

Note that y['myfloat'] gives floats because of the dtype argument, even though in the file they are ints.

或者,您可以使用整数以给定的dtype将行作为tuple:

Or, you can use an integer to get a row as a tuple with the given dtype:

>>> y[1]
('string002', 456.0)

如果您要对这样的数据结构进行大量处理,则可能需要研究 pandas

If you are doing a lot of manipulation of data structures like this, you might want to look into pandas

这篇关于使用genfromtxt分割数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆