# Calculate mean values for all bands and date to a dataframe

I have 5 rasters, each having 10 bands, where each band corresponds to a specific date.

How do I calculate mean values for all 10 bands and save them?

I need to save the mean values in a dataframe like this example:

``````   rasters   bands       mean         date             years
raster1   band1       1234         24/01/2000       2000
band2       45213        01/03/2000       2000
band3        4221        12/04/2000       2000
band4         ...         ...              ....
band10
raster 2   band 1       4521        01/02/2001      2001
band2        1234        04/05/2001      2001
...           ....         ....
``````

This script calculates mean value for each band in my raster:

``````raster = gdal.Open("D:/script/NDVI2000.tif")
bands = raster.RasterCount

for band in range(1, bands+1):
mean = np.mean(data[data != 0]) #calculate mean without value 0
print("Band %s: Mean = %s" % (band, round(mean, 2)))
Out:
Band 1: Mean = 1712.83
Band 2: Mean = 1803.14
Band 3: Mean = 1662.33
Band 4: Mean = 1868.77
Band 5: Mean = 1900.97
Band 6: Mean = 2031.13
Band 7: Mean = 1847.89
Band 8: Mean = 2185.66
Band 9: Mean = 2081.14
Band 10: Mean = 8248.7
``````

How can I save the mean result stats in the dataframe using gdal python 3 after I have the results for all rasters?

I need this dataframe to plot times series for NDVI.

• Where does the `date` data come from in your output example? Jan 16 '19 at 19:09
• How are you acquiring the date and year information? How do you hope to incorporate time into the dataframe?
– Aaron
Jan 16 '19 at 19:50
• @Aaron before doing rasterstack in ENVI i hade the bands with date ( one bands for one date ) exemple : MOD13Q1.006__250m_16_days_EVI_doy2000209_aid0001 first band for julianday 209 = 01/07/2000 so i'm doing srack for all bands in the same years with ordre just in ENVI 5.2 cause i don't know how doing stack in gdal Python 3 and how to export the date from name of band to dataframe Jan 16 '19 at 20:09
• @Aaron i know the date for all bands in all rasters so just need to add date in dataframe i think ! Jan 16 '19 at 21:41

Something like this, append dicts to an empty list. It seems from your example output, you may want to do this inside a function that you can pass the path of the target raster. Note the `???` means you will have to implement something. Maybe your year comes from the filename?

``````import pandas as pd

raster_filename = "D:/script/NDVI2000.tif"
raster = gdal.Open(raster_filename)
bands = raster.RasterCount

row_list = []

for band in range(1, bands+1):
mean = np.mean(data[data != 0]) #calculate mean without value 0
row_list.append({
'raster': os.path.basename(raster_filename),
'band': band,
'mean': mean,
'year': ???,
'date': ???,
})
print("Band %s: Mean = %s" % (band, round(mean, 2)))

# create DataFrame with columns from the dict keys
df = pd.DataFrame(row_list)
df.to_csv('band_means.csv')

``````

Good luck!

• thank u for answer i will test it and undersant and if i want to do it for 5 rasters ( 10 bans in the rasters ) and all the raster in the same folder NDVI2000, NDVI2001 , NDVI2003 ,... Jan 16 '19 at 20:02
• I'd recommend Python's `os.scandir()` to loop over the files Jan 16 '19 at 20:21
• i will try it sincerly cause i need to undersan thank u Jan 16 '19 at 20:32
• `date': ???` i don't know how can writhe `date:01/02/2000` Jan 16 '19 at 20:54
• `date : 01-02-2000 ^ SyntaxError: invalid token` Jan 16 '19 at 20:57