Reputation: 37
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 ?
Upvotes: 1
Views: 1572
Reputation: 15442
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.
Upvotes: 1