Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Upload csv file as numbers (floating?) and extract element, row, and column
#1
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
Quote
#2
I modified you code, and removed absolute link.
Could you please give a sample of matrix.csv (enough for a complete entry)
Quote
#3
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
Quote
#4
That's not a CSV (Comma Separated Values) file!
There are no comma's!
nilamo likes this post
Quote
#5
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.
Quote
#6
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#7
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
Quote
#8
I think I did a better job of posting on my next question:

https://python-forum.io/Thread-Convert-i...and-pandas

if you want to give it a shot.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Change column names from a file Nidhesh 2 355 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  How to delete column if entire column values are "nan" Sri 4 594 Apr-13-2019, 12:16 PM
Last Post: Sri
  OpenCV - extract 1st frame out of a video file kerzol81 2 2,334 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  copy one column from csv file and paste into xls file kprogrammer 0 507 Nov-03-2018, 04:03 PM
Last Post: kprogrammer
  Writing a floating point aifc file jandyman 1 884 Dec-29-2017, 07:48 PM
Last Post: Larz60+
  Extract data between two dates from a .csv file using Python 2.7 sujai_banerji 1 4,608 Nov-15-2017, 09:48 PM
Last Post: snippsat
  Identifying items in a csv file that also appear in a Text extract Jaynorth 17 5,864 Sep-21-2016, 10:51 PM
Last Post: Jaynorth

Forum Jump:


Users browsing this thread: 1 Guest(s)