Search code examples
pythonpandasdataframeraster

Python 3: Create raster with pixel values sourced from a pandas dataframe


I need to create a 328 x 238 (row x column) raster where select pixel/cell values are populated from a pandas dataframe (df) that I've been given. Each row within the df contains a name (not important), row number, column number, and pixel value that corresponds to some (not all) of the 328 x 238 raster cells. The pixel value needs to be an 'NaN' or empty if no cell value exists. I'm new to python and am struggling with figuring out what would be the best approach to get to my end goal. First thought would be to loop through each row in the df to extract the row/column location and the value that each pixel should be populated with, if one exists in the df.

As a first step I was thinking of creating a 328 x 238 numpy array of NaN values:

array = np.full((328, 238), 'NaN')

I was then thinking that I could use a for loop to populate the array with values from the df. However, I'm still struggling to understand how to set up a for loop that would read through the df line by line and populate the array with specific values from the df at each listed row/column location.

for row, col in array:
    array.append[df['row', 'column'], 'value']

This for loop obviously doesn't work, but I dont know how to get that 'value' out of the df and populate the array at that specific 'row' and 'column' location. Any help in directing me down the best route to achieve the end goal of a raster would be greatly appreciated.


Solution

  • You can iterate over the rows of the dataframe using df.iterrows

    for index, row in df.iterrows():
         array[row['row_number'], row['column_number']] = row['value']