1

I have a CSV file of 625 rows like this-

X_coordinate,Y_coordinate,Value
0,0,10
0,1,6
0,2,7
.
.
1,0,11
1,1,9
1,2,3
.
.
24,23,3
24,24,12

I want to convert this file into a numpy array of 25*25, where array indices contain corresponding values from CSV like-

[[10 6 7....]
 [11 9 3....]
 .
 .
 .
 [......3 12]]

How can I go about scripting this?

1
  • 4
    What have you tried so far? We can't just do it for you... Commented Nov 29, 2017 at 2:57

1 Answer 1

3

You can keep the column names if you use the names=True argument in np.genfromxt. Following code assumes the Value is int:

import numpy as np
data = np.genfromtxt(path_to_csv, dtype=int, delimiter=',', names=True)

Read CSV file to numpy array, first row as strings, rest as float

Then, assign a values for each pair of indices.

m = np.zeros((25, 25))
for x, y, value in data:
    m[x, y] = value
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.