I have a 10-year sst monthly data. I want to plot the interannual monthly climatology for the month of October. By using the code I have for python with me I am able to plot the interannual monthly climatology for all the 12 months. If anyone can help me by going over my code and say where I have to put index so that I can have monthly climatology only for the month of October it will be much appreciated. Below is my code:

import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
#time_slice = slice(9,10)
lat_slice = slice(30, 0)
lon_slice = slice(40, 80)
nc = xr.open_dataset('E:/X49.')
ds = nc.groupby('time.month', squeeze = False).mean('time')
dt = ds.sst.sel(lat = lat_slice,lon =lon_slice)
dt.plot.pcolormesh(x = 'lon', y = 'lat', col = 'month', col_wrap =3) 

1 Answer 1

ds = nc.groupby('time.month', squeeze = False).mean('time')

Here, you may put a "filter" either before or after group by.

Either you can do this-

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                          'foo', 'bar'],
                   'B' : [1, 2, 3, 4, 5, 6],
                   'C' : [2.0, 5., 8., 1., 2., 9.]})
grouped = df.groupby('A')
grouped.filter(lambda x: x['B'].mean() > 3.)

Source - https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.filter.html

Or something like this-

gdf.apply(lambda g: g[g['team'] == 'A']).reset_index(drop=True).groupby(gdf.grouper.names)

Source- https://stackoverflow.com/questions/27488080/python-pandas-filter-rows-after-groupby


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.