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

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,
I modified you code, and removed absolute link.
Could you please give a sample of matrix.csv (enough for a complete entry)
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?

That's not a CSV (Comma Separated Values) file!
There are no comma's!
nilamo likes this post
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.
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()
You should look into string-methods.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
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,
I think I did a better job of posting on my next question:

if you want to give it a shot.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 153 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  How to print a column name in csv file Truman 1 259 Mar-31-2020, 03:34 AM
Last Post: Larz60+
  Change column names from a file Nidhesh 2 733 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  How to delete column if entire column values are "nan" Sri 4 1,010 Apr-13-2019, 12:16 PM
Last Post: Sri
  OpenCV - extract 1st frame out of a video file kerzol81 2 4,818 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  copy one column from csv file and paste into xls file kprogrammer 0 1,141 Nov-03-2018, 04:03 PM
Last Post: kprogrammer
  Writing a floating point aifc file jandyman 1 1,057 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 5,952 Nov-15-2017, 09:48 PM
Last Post: snippsat
  Identifying items in a csv file that also appear in a Text extract Jaynorth 17 6,887 Sep-21-2016, 10:51 PM
Last Post: Jaynorth

Forum Jump:

Users browsing this thread: 1 Guest(s)