ynitSed - 1 month ago 13

Python Question

new to programming here.

Some context: Opened a very large csv file, removed the unnecessary data in first few lines using a bit of code (can see previous post to see code).

Now i'd like to create a matrix. To determine matrix size, I resorted to using the max value of one column and the max value of another column. This is my code so far, however I couldn't get it to work for some odd reason:

`firstcolumn_max = max(readcsv, key=op.itemgetter(0))`

secondcolumn_max = max(readcsv, key=op.itemgetter(1))

matrix_size = max(firstcolum_max, secondcolumn_max)

Next step: creating a zero matrix such as

`matrix=[[0 for i in range(matrix_size)] for i in range(matrix_size)]`

Following this, I'd like to populate the matrix: (this is where I get stuck). I'd like to populate that matrix using like, data in the 4th column in that CSV file but wasn't able to. Was thinking of writing it to a output excel file.

`for i in readcsv:.........`

However, everything (most things) that I do seem to go wrong.

Answer

You can try and use panda, specifically this function, as_matrix(), to convert it to a matrix/array:

If you're completely new to panda, their tutorial is pretty good and doesn't take long!

You can read from a csv which will return a DataFrame. From that dataframe you can use the function, as_matrix(), I listed earlier.