Create pandas DataFrame from raster image - one row per pixel with bands as columns
Quick solution
pd.DataFrame(array.reshape([3,-1]).T)
Explanation
- 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])
- Transpose array to get array of shape
(x*y, 3)
transposed_array = reshaped_array.T
- Build DataFrame
pd.DataFrame(transposed_array)