Loading mixed data from csv to numpy array in python

I have a csv file containing 10 columns and 6 rows that I want to convert to a numpy array. Although it is loading, I cannot use the data right now, and I think I am missing a step.

My code now looks like this

import numpy as np

filename = "test_ioc.csv"

# open file
f=open(filename)

# initialize this 
myfile = [] 

# Convert to numpy array
mat = np.vstack([signal for signal in f.readlines()])
print mat

I also did this:

import numpy as np

filename = "test_ioc.csv"

# open file
f=open(filename)

# initialize this 
myfile = [] # empty nested list that is the big container, this contains all the rows

# f.readlines, and for each line,
for line in f.readlines():

#create a list for each row
row = [] # empty list for row items, each row has 2 lists

# line.strip, line.split, and for each i in this:
for eye in line.strip().split():

    # convert elements into floats

    row.append(eye) # append each item to list 'row'


# append all the parts row to the list myfile that you created
myfile.append(row) #append list to my file

print myfile

# now that you have your gigantic list myfile, convert to it to numpy array
a = np.array(myfile) #convert the list into a numpy array

# slice accordingly!
x = a[:,0] #first column
y = a[:,1] #second column
f.close() 

The first gives me this conclusion:

print a
    [['2043l0.wav,0.115,0.169,0.222,0.23,2043l0.wav,0.21,0.169,0.238,0.23']
 [ 'dn2001l0.wav,0.105,0.161,0.242,0.222,dn2001l0.wav,0.153,0.176,0.207,0.207']
 ['2694l0.wav,0.13,0.192,0.33,0.314,2694l0.wav,0.192,0.184,0.207,0.238']
 ['2641l0.wav,0.123,0.146,0,0.407,2641l0.wav,0.199,0.199,0.199,0.176']
 ['2622l0.wav,0.284,0.353,0.582,0.582,2622l0.wav,0.268,0.161,0.176,0.184']
 ['dn2047l0.wav,0.12,0.23,0.368,0.322,dn2047l0.wav,0.369,0.169,0.207,0.222']]

I really need to divide my lines further into 2 sets of 4, convert each number in each line to float, but I'm new to python and just want to be able to perform some basic operations on my data and draw them using Matplotlib. Thank you for your help!

+4
source share
2 answers

, , , , , :

mat = np.vstack([signal.split(",") for signal in f)])

csv lib :

import  csv
mat = np.vstack(csv.reader(f))))

np.loadtxt:

import  numpy as np

arr = np.loadtxt("in.csv",delimiter=",",dtype=object)

print(arr)

:

[['2043l0.wav' '0.115' '0.169' '0.222' '0.23' '2043l0.wav' '0.21' '0.169'
  '0.238' '0.23']
 ['dn2001l0.wav' '0.105' '0.161' '0.242' '0.222' 'dn2001l0.wav' '0.153'
  '0.176' '0.207' '0.207']
 ['2694l0.wav' '0.13' '0.192' '0.33' '0.314' '2694l0.wav' '0.192' '0.184'
  '0.207' '0.238']
 ['2641l0.wav' '0.123' '0.146' '0' '0.407' '2641l0.wav' '0.199' '0.199'
  '0.199' '0.176']
 ['2622l0.wav' '0.284' '0.353' '0.582' '0.582' '2622l0.wav' '0.268' '0.161'
  '0.176' '0.184']
 ['dn2047l0.wav' '0.12' '0.23' '0.368' '0.322' 'dn2047l0.wav' '0.369'
  '0.169' '0.207' '0.222']]

genfromtxt, , .

import numpy as np

headings = [('filename1', "|S20"), ('l0', float), ('l1', float), ('l2', float), ('l3', float),
            ('filename2', "|S10"), ('r0', float), ('r1', float), ('r2', float), ('r3', float)]

arr = np.genfromtxt("in.csv", delimiter=",", dtype=headings)

print(arr)
[ ('2043l0.wav', 0.115, 0.169, 0.222, 0.23, '2043l0.wav', 0.21, 0.169, 
0.238, 0.23)
 ('dn2001l0.wav', 0.105, 0.161, 0.242, 0.222, 'dn2001l0.w', 0.153, 0.176, 0.207, 0.207)
 ('2694l0.wav', 0.13, 0.192, 0.33, 0.314, '2694l0.wav', 0.192, 0.184, 0.207, 0.238)
 ('2641l0.wav', 0.123, 0.146, 0.0, 0.407, '2641l0.wav', 0.199, 0.199, 0.199, 0.176)
 ('2622l0.wav', 0.284, 0.353, 0.582, 0.582, '2622l0.wav', 0.268, 0.161, 0.176, 0.184)
 ('dn2047l0.wav', 0.12, 0.23, 0.368, 0.322, 'dn2047l0.w', 0.369, 0.169, 0.207, 0.222)]

, pandas, arr["filename1"] ..

+1

, , , pandas.

import pandas as pd
filename = 'test_ioc.csv'
headings = 'filename', 'l0', 'l1', 'l2', 'l3', 'filename', 'r0', 'r1', 'r2', 'r3'

#data
data = pd.read_csv(filename, names=headings)

print data
+2

All Articles