Python Forum
Upload csv file as numbers (floating?) and extract element, row, and column
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
Reply
#2
I modified you code, and removed absolute link.
Could you please give a sample of matrix.csv (enough for a complete entry)
Reply
#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
Reply
#4
That's not a CSV (Comma Separated Values) file!
There are no comma's!
Reply
#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.
Reply
#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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding the median of a column in a huge CSV file markagregory 5 1,732 Jan-24-2023, 04:22 PM
Last Post: DeaD_EyE
  extract and plot data from a txt file usercat123 2 1,208 Apr-20-2022, 06:50 PM
Last Post: usercat123
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,242 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  How to print a column name in csv file Truman 1 4,366 Mar-31-2020, 03:34 AM
Last Post: Larz60+
  Change column names from a file Nidhesh 2 2,961 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  OpenCV - extract 1st frame out of a video file kerzol81 2 21,759 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  copy one column from csv file and paste into xls file kprogrammer 0 4,342 Nov-03-2018, 04:03 PM
Last Post: kprogrammer
  Writing a floating point aifc file jandyman 1 2,597 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 10,307 Nov-15-2017, 09:48 PM
Last Post: snippsat
  Identifying items in a csv file that also appear in a Text extract Jaynorth 17 17,257 Sep-21-2016, 10:51 PM
Last Post: Jaynorth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020