When looking at highly dimensional data it is easy to become overwhelmed with the possibilities of how you can look at, slice, and analyze the data to actually answer a question from that sea of numbers. Group by is a common operation we need to do and can easily reduce the complexity of the dataset for analysis. In geology we may want to group facies by a property like color, gamma, or seismic velocity. In meteorology we may want to do some sort of spatial grouping. Luckily, XArray makes these operations easy. If you’re not familiar with XArray, there are other posts you can checkout to get started. If you are used to working with Pandas, this post on converting dataframes to datasets may be useful!

In this example we’ll use some example data from the MetPy library. The data is from the NARR (North American Regional Reanalysis) and has a small set of temperatures at 29 levels in the atmosphere. After reading the data in and a little bit of cleanup, we can quickly group the temperatures into bins of 2 degrees of latitude, average in all other dimensions and make a plot! It just takes a single line (okay, okay, with some chained operators) to do it.

If you find these Python tips useful, be sure to checkout the great work done for Earth scientists by the team over and Unidata! Also don’t forget that we offer Python training classes online, at your institution, or in our classroom in beautiful northwest Arkansas!

John Leeman
Follow me