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?

amadispstac
  • 687
  • 1
  • 5
  • 6

1 Answers1

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
dkato
  • 895
  • 10
  • 28