Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load csv into 2D matrix with numpy for plotting

Given this CSV file:

"A","B","C","D","E","F","timestamp" 611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12 611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12 611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12 

I simply want to load it as a matrix/ndarray with 3 rows and 7 columns. However, for some reason, all I can get out of numpy is an ndarray with 3 rows (one per line) and no columns.

r = np.genfromtxt(fname,delimiter=',',dtype=None, names=True) print r print r.shape  [ (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291111964948.0)  (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291113113366.0)  (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291120650486.0)] (3,) 

I can manually iterate and hack it into the shape I want, but this seems silly. I just want to load it as a proper matrix so I can slice it across different dimensions and plot it, just like in matlab.

like image 417
dgorissen Avatar asked Nov 30 '10 15:11

dgorissen


People also ask

How do I convert a CSV file to a matrix in Python?

Line 1: We import the NumPy library. Line 3-4: We open the sampleCSV file and we pass both CSVData and the delimiter to NumPy np. genfromtxt () function, which returns the data into a 2D array. Line 6: We finally print the result which shows that now our CSV data converted into a 2D array.

How do I convert a CSV file to a NumPy array in Python?

We can use a dataframe of pandas to read CSV data into an array in python. We can do this by using the value() function. For this, we will have to read the dataframe and then convert it into a numpy array by using the value() function from the pandas' library.

How do I import a CSV file into an array?

Use numpy. loadtxt() to Read a CSV File Into an Array in Python. As the name suggests, the open() function is used to open the CSV file. NumPy's loadtxt() function helps in loading the data from a text file.

How do I save a 2D NumPy array as a CSV?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.


2 Answers

Pure numpy

numpy.loadtxt(open("test.csv", "rb"), delimiter=",", skiprows=1) 

Check out the loadtxt documentation.

You can also use python's csv module:

import csv import numpy reader = csv.reader(open("test.csv", "rb"), delimiter=",") x = list(reader) result = numpy.array(x).astype("float") 

You will have to convert it to your favorite numeric type. I guess you can write the whole thing in one line:

 result = numpy.array(list(csv.reader(open("test.csv", "rb"), delimiter=","))).astype("float") 

Added Hint:

You could also use pandas.io.parsers.read_csv and get the associated numpy array which can be faster.

like image 119
Kaveh_kh Avatar answered Sep 30 '22 08:09

Kaveh_kh


I think using dtype where there is a name row is confusing the routine. Try

>>> r = np.genfromtxt(fname, delimiter=',', names=True) >>> r array([[  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,           8.64075140e+02,   1.71537476e+03,   7.65227770e+02,           1.29111196e+12],        [  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,           8.64075140e+02,   1.71537476e+03,   7.65227770e+02,           1.29111311e+12],        [  6.11882430e+02,   9.08956010e+03,   5.13300000e+03,           8.64075140e+02,   1.71537476e+03,   7.65227770e+02,           1.29112065e+12]]) >>> r[:,0]    # Slice 0'th column array([ 611.88243,  611.88243,  611.88243]) 
like image 23
mtrw Avatar answered Sep 30 '22 09:09

mtrw