I have a raster image with 3 bands. I would like to convert this image to a csv
file where each row will be one pixel and each column will be one band, so that I can easily see the three values each pixel got.
This is how I have tried to do it:
import rasterio
import rasterio.features
import rasterio.warp
from matplotlib import pyplot
from rasterio.plot import show
import pandas as pd
import numpy as np
img=rasterio.open("01032020.tif")
show(img,0)
#read image
array=img.read()
#create np array
array=np.array(array)
#create pandas df
dataset = pd.DataFrame({'Column1': [array[0]], 'Column2': [array[1]],'Column3': [array[2]]})
dataset
and also like this:
dataset = pd.DataFrame({'Column1': [array[0,:,:]], 'Column2': [array[1,:,:]],'Column3': [array[2:,:]]})
but i'm getting something weird like this table:
I have also tried:
index = [i for i in range(0, len(array[0]))]
dataset = pd.DataFrame({'Column1': array[0], 'Column2': array[1],'Column3': array[2]},index=index)
dataset
but then I get the number of the rows I have and it's still not good:
what do I do wrong?
My goal
Get one pandas table, where each row is a pixel, and it should have 3 columns, one for each band.