简体   繁体   中英

Using Row/Column values from a table to print corresponding location value into array?

I have a csv file with headers titled Row, Column, and ID number. There are a total of 10 rows, 15 columns (making a total of 165 entries), each with a corresponding ID number specific to the location on the 10x15 grid surface Snip . Using the specific row/column values, I'd like to fill an array with the corresponding ID number to the specified location in the grid. So far I have created a 10x15 array of zeros and would like to replace those values with the ID numbers at the correct location. Fairly new to python so apologies if this is not explained great, but any advice is appreciated!

Assuming your ids are integers, you could just store them into a numpy array. You could use

id_array = np.genfromtxt('your_file.csv', delimiter = ',')

or else used pandas library

import pandas as pd

df=pd.read_csv('myfile.csv')

you can convert it into numpy using df.to_numpy()

Your id array will be a 3D array just, iterate over its rows to populate your grid array.

 for row in range(id_array.shape[0]): 
      grid_array[id_array[row][0]][id_array[row][1]] = id_array[row][2]

Or else you can do this also,

rows = id_array[:,0]
cols = id_array[:,1]
grid_arr[rows,cols] = id_array[:,2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM