Skip to content

Convert raster to dataframe

raster_to_dataframe(raster, bands=None, add_coordinates=False)

Convert raster to Pandas DataFrame.

If bands are not given, all bands are used for conversion. Selected bands are named based on their index e.g., band_1, band_2,...,band_n. If wanted, image coordinates (row, col) for each pixel can be written to dataframe by setting add_coordinates to True.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be converted.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None
add_coordinates bool

Determines if pixel coordinates are written into dataframe. Defaults to False.

False

Returns:

Type Description
DataFrame

Raster converted to a DataFrame.

Source code in eis_toolkit/conversions/raster_to_dataframe.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@beartype
def raster_to_dataframe(
    raster: rasterio.io.DatasetReader,
    bands: Optional[Sequence[int]] = None,
    add_coordinates: bool = False,
) -> pd.DataFrame:
    """Convert raster to Pandas DataFrame.

    If bands are not given, all bands are used for conversion. Selected bands are named based on their index e.g.,
    band_1, band_2,...,band_n. If wanted, image coordinates (row, col) for each pixel can be written to
    dataframe by setting add_coordinates to True.

    Args:
        raster: Raster to be converted.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.
        add_coordinates: Determines if pixel coordinates are written into dataframe. Defaults to False.

    Returns:
        Raster converted to a DataFrame.
    """

    data_frame = _raster_to_dataframe(
        raster=raster,
        bands=bands,
        add_coordinates=add_coordinates,
    )
    return data_frame