'Reading multiple raster tif as dataframe
I want to read several raster tif files and get a dataframe as output. Each raster has two bands.
I want to get something like this as output:

I tried something to read all tif files but I don't know how stack them as dataframe :
import numpy as np
import glob
import rasterio as rio
final = []
tif_files = glob.glob(os.path.join(Repo_img, r'*.tif'))
for f in tif_files:
im = rio.open(f).read(1)
imarray = np.array(im)
final.append(imarray)
final = np.asarray(final)
Anyone can help please ?
Solution 1:[1]
Replacing your last line with the following should do the trick
final = pd.DataFrame(
# concatenate column vectors
np.hstack([
# first flatten, then convert row vectors to columns
f.ravel().reshape(-1, 1)
# for each array in your list
for f in final
])
)
See the numpy beginner's guide to reshaping arrays for more info.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Delgado |
