473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Import .csv to numpy instead of list to numpy

Currently when I import a data file, I create a list, and then use the
Expand|Select|Wrap|Line Numbers
  1. numpy.array(lst)
method to convert the list into a numpy array.

However, ideally I would like to not have to use a transition data type before converting it to a numpy array (and import the .csv file directly into a numpy array).

I have tried the
Expand|Select|Wrap|Line Numbers
  1. numpy.genfromtext()
method, however, that does not keep the inherent matrix structure I am attempting to preserve (it creates a single vector instead of maintaining the distinct columns and rows from the .csv).

My import method basically looks like this:
Expand|Select|Wrap|Line Numbers
  1. import csv
  2. f = open('fileName.csv','rb')
  3. rdr = csv.reader(f,delimeter=',')
  4. lst = []
  5. for row in rdr:
  6. lst.append(row)
When I try to add something like:
Expand|Select|Wrap|Line Numbers
  1. numArray = numpy.array([])
  2. for row in rdr:
  3. numpy.append(numArray,row)
However, I am finding that the result is just an empty numpy array at the completion of the loop.

I have also tried
Expand|Select|Wrap|Line Numbers
  1. numpy.row_stack
, however it seems to me like the number of columns must be known apriori to use this (and I don't know how to use the
Expand|Select|Wrap|Line Numbers
  1. csv.reader()
to determine the number of columns.

Any ideas / assistance you could provide would be most greatly appreciated.!!!
Jun 5 '11 #1
7 10056
bvdet
2,851 Expert Mod 2GB
The following bypasses the intermediate assignment to lst and typecasts each str element to float.
Expand|Select|Wrap|Line Numbers
  1. import csv
  2. import numpy
  3.  
  4. f = open("array.txt")
  5.  
  6. numArray = numpy.array([map(float, item) for item in csv.reader(f)])
  7.  
  8. f.close()
Jun 6 '11 #2
Thanks bvdet!

If have different types in each column, (say of string() and float()) is there any easy way for me to use the map functionality in the list comprehension you provided to account for the different data types?

Thanks for your help!
Jun 6 '11 #3
bvdet
2,851 Expert Mod 2GB
Encapsulate the creation of the array in a function with the data type as an argument.
Expand|Select|Wrap|Line Numbers
  1. def create_array(fileObj, dataType):
  2.     return numpy.array([map(dataType, item) for item in csv.reader(fileObj)])
Jun 6 '11 #4
bvdet
2,851 Expert Mod 2GB
Oops, I misread your previous message. I am no expert on Numpy, but a Numpy array can only have one data type. Why not create a list of lists or your own container object?
Jun 7 '11 #5
I starting off using a list of lists. However, I need to pass pairs of vectors (a date column along with a float column) into another function for processing.

In a numpy array, this consist of:
Expand|Select|Wrap|Line Numbers
  1. numpyArray[:,0:2]
It doesn't seem like there's as clean a way to do this with lists when I'm reading in an entire matrix of data. That's why i was thinking that the numpy.array() was probably the correct data type for me to use.
Jun 7 '11 #6
bvdet
2,851 Expert Mod 2GB
You can create a class to contain your list of lists. If you have __getitem__ and __iter__ overloads, the information you want to pass could be accessed like this:
Expand|Select|Wrap|Line Numbers
  1. [item[0:2] for item in arrayObj]
It should work the same for a list of lists.
Jun 7 '11 #7
I'm fairly new to Python (programming) so I don't feel all that comfortable creating classes right now. Thanks so much for your input and suggestions.
Jun 7 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: cesco | last post by:
Hi, I'm trying to install the numpy library (precisely numpy-0.9.6-py2.4-linux-i686) on Linux but I encounter several problems. After unpacking the file it creates the following folders:...
3
by: Iljya | last post by:
Hello, I need to pickle the type numpy.float32 but encounter an error when I try to do so. I am able to pickle the array itself, it is specifically the type that I cannot pickle. I am using:...
5
by: auditory | last post by:
I am a newbie here I am trying to read "space separated floating point data" from file I read about csv module by searching this group, but I couldn't read space separated values with csv....
3
by: Duncan Smith | last post by:
Hello, Since moving to numpy I've had a few problems with my existing code. It basically revolves around the numpy scalar types. e.g. ------------------------------------------------ array(,...
1
by: adolfo | last post by:
I downloaded and installed Phyton 2.52 (it works), numpy-1.0.4.win32- py2.5, and scipy-0.6.0.win32-py2.5 I can´t get Numpy to show up at Python´s IDLE, or command line. If I do: # I get...
5
by: Marc Oldenhof | last post by:
Hello all, I'm pretty new to Python, but use it a lot lately. I'm getting a crazy error trying to do operations on a string list after importing numpy. Minimal example: Python 2.5.1...
2
by: sapsi | last post by:
Hello, I have a numpy array (2 rows 3 colums) import numpy a=numpy.array( , ]) I wish to add a row, this is how i do it s=a.shape numpy.resize(a,s+1,s)
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
3
by: jrh | last post by:
Hello, From previous posts and documentation it seems python should be able to import a module that is compiled into a .dll just as well as a .pyd. I have a pyd that works fine, but after...
3
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.