Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import data with different types from file into a Python Numpy array?

Tags:

python

numpy

Say I have a file myfile.txt containing:

1   2.0000  buckle_my_shoe
3   4.0000  margery_door

How do I import data from the file to a numpy array as an int, float and string?

I am aiming to get:

array([[1,2.0000,"buckle_my_shoe"],
[3,4.0000,"margery_door"]])

I've been playing around with the following to no avail:

a = numpy.loadtxt('myfile.txt',dtype=(numpy.int_,numpy.float_,numpy.string_))

EDIT: Another approach might be to use the ndarray type and convert afterwards.

b = numpy.loadtxt('myfile.txt',dtype=numpy.ndarray)

    array([['1', '2.0000', 'buckle_my_shoe'],
       ['3', '4.0000', 'margery_door']], dtype=object)
like image 552
atomh33ls Avatar asked Mar 18 '13 16:03

atomh33ls


People also ask

Can a Numpy array hold data of different data types?

Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.

Can Numpy array have mixed data types?

Having a data type (dtype) is one of the key features that distinguishes NumPy arrays from lists. In lists, the types of elements can be mixed.

Can Numpy contain elements of different types?

Yes, if you use numpy structured arrays, each element of the array would be a "structure", and the fields of the structure can have different datatypes.


2 Answers

Use numpy.genfromtxt:

import numpy as np
np.genfromtxt('filename', dtype= None)
# array([(1, 2.0, 'buckle_my_shoe'), (3, 4.0, 'margery_door')], 
#       dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '|S14')])
like image 66
root Avatar answered Sep 23 '22 07:09

root


Pandas can do that for you. The docs for the function you could use are here.

Assuming your columns are tab separated, this should do the trick (adapted from this question):

df = DataFrame.from_csv('myfile.txt', sep='\t')
array = df.values # the array you are interested in
like image 39
mtth Avatar answered Sep 20 '22 07:09

mtth