Python Forum
Upload csv file as numbers (floating?) and extract element, row, and column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Upload csv file as numbers (floating?) and extract element, row, and column (/thread-9000.html)



Upload csv file as numbers (floating?) and extract element, row, and column - bentaz - Mar-16-2018

Hello,

I've been trying for a couple days to upload a csv file as a 2D matrix of numerical values (e.g. 1+2=3) so that I can extract individual numbers, rows, and, columns. I am using numpy. The input csv file is has four rows and three columns.

This is as close as I've gotten:

import numpy as np
import csv 
with open('C:\\Users\\...\\matrix.csv') as File:  
    reader = csv.reader(File, delimiter=',')
    for data in reader:  
        data = np.matrix(data, dtype=float)
        x = data.shape        
        print (x)
This gives me four 1x3 matrices instead of a 4x3 matrix. Thoughts?

Thank you,
Ben


RE: Upload csv file as numbers (floating?) and extract element, row, and column - Larz60+ - Mar-16-2018

I modified you code, and removed absolute link.
Could you please give a sample of matrix.csv (enough for a complete entry)


RE: Upload csv file as numbers (floating?) and extract element, row, and column - bentaz - Mar-16-2018

Hi Larz,

The matrix.csv is just:

1 5 9
2 6 10
3 7 11
4 8 12

Is there any way to upload files here?

Thanks


RE: Upload csv file as numbers (floating?) and extract element, row, and column - Larz60+ - Mar-16-2018

That's not a CSV (Comma Separated Values) file!
There are no comma's!


RE: Upload csv file as numbers (floating?) and extract element, row, and column - bentaz - Mar-16-2018

That is an excellent point! That's why I asked about uploading files.

That matrix (or 2D array if you prefer) is saved in a .csv spreadsheet and then uploaded into Python.

I.e. copy and paste those numbers into an excel file and save as a .csv file.

I used this instead:

import pandas as pd
import numpy as np
filename = 'C:\\Users\\bpt6\\Desktop\\OxideThicknessTest2\\matrix2.csv'
data = pd.read_csv(filename, header = None);
data = data.values ;  ###Convert DataFrame to array of values (numbers)
data = data.astype(np.float); ###Convert for scientific notation to floating
Thanks anyway.


RE: Upload csv file as numbers (floating?) and extract element, row, and column - DeaD_EyE - Mar-16-2018

Uploaded is the wrong description what you want to do.

You're opening a file and reading from a file. This has nothing to do with upload.
I guess you copied some snippets from some places and you don't know what you're doing.

If you want to split a space separated table, use the split method.

with open('your-file.text') as fd:
    for line in fd:
        rows = line.split()
        print(rows)
You should look into string-methods.


RE: Upload csv file as numbers (floating?) and extract element, row, and column - bentaz - Mar-16-2018

My apologies for using the word "upload" instead of "open and read" in the first post. Egg on my face for sure.

After that I WAS asking how to upload files (a .csv file) to this forum of kind souls. The space separated example matrix I posted was to show what I meant by the 4x3 matrix which I described in the post, since matrix means different things in different languages. I thought the commas were implied. More egg on my face.

You're right. I don't know what I'm doing in Python. If I did I wouldn't be asking what should be simple questions. I'm trying to convert programs from MATLAB. I guess MATLAB made me soft since all it requires is:

X = importdata('C:\Desktop\matrix.csv');

Where's the fun in that, right?

Thanks for your help,
Ben


RE: Upload csv file as numbers (floating?) and extract element, row, and column - bentaz - Mar-19-2018

I think I did a better job of posting on my next question:

https://python-forum.io/Thread-Convert-indexing-For-Loop-from-MATLAB-uses-numpy-and-pandas

if you want to give it a shot.