Carl Bethuel
Carl Bethuel

Reputation: 37

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: enter image description here

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

Answers (1)

Michael Delgado
Michael Delgado

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

Related Questions