Create pandas DataFrame from raster image - one row per pixel with bands as columns

Quick solution



  1. Take array of shape (3, x, y) and flatten out the 2nd and 3rd dimension. From the numpy docs: One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
reshaped_array = array.reshape([3,-1])
  1. Transpose array to get array of shape (x*y, 3)
transposed_array = reshaped_array.T
  1. Build DataFrame