{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 2.3: Long-term trends in hybridization of Darwin finches\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Peter and Rosemary Grant](https://en.wikipedia.org/wiki/Peter_and_Rosemary_Grant) have been working on the Galápagos island of Daphne Major for over forty years. During this time, they have collected lots and lots of data about physiological features of finches. In 2014, they published a book with a summary of some of their major results (Grant P. R., Grant B. R., *40 years of evolution. Darwin's finches on Daphne Major Island*, Princeton University Press, 2014). They made their data from the book publicly available via the [Dryad Digital Repository](http://dx.doi.org/10.5061/dryad.g6g3h).\n", "\n", "We will investigate their measurements of beak depth (the distance, top to bottom, of a closed beak) and beak length (base to tip on the top) of Darwin's finches. We will look at data from two species, *Geospiza fortis* and *Geospiza scandens*. The Grants provided data on the finches of Daphne for the years 1973, 1975, 1987, 1991, and 2012. I have included the data in the files `grant_1973.csv`, `grant_1975.csv`, `grant_1987.csv`, `grant_1991.csv`, and `grant_2012.csv`. They are in almost exactly the same format is in the Dryad repository; I have only deleted blank entries at the end of the files.\n", "\n", "**Note**: If you want to skip the wrangling (which is very valuable experience), you can go directly to part (d). You can load in the data frame you generate in parts (a) through (c) from the file `~/git/bootcamp/data/grant_complete.csv`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a)** Load each of the files into separate Pandas data frames. You might want to inspect the file first to make sure you know what character the comments start with and if there is a header row." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b)** We would like to merge these all into one data frame. The problem is that they have different header names, and only the 1973 file has a year entry (called `yearband`). This is common with real data. It is often a bit messy and requires some wrangling. \n", "\n", "1. First, change the name of the `yearband` column of the 1973 data to `year`. Also, make sure the year format is four digits, not two! \n", "2. Next, add a `year` column to the other four data frames. You want tidy data, so each row in the data frame should have an entry for the year.\n", "3. Change the column names so that all the data frames have the same column names. I would choose column names\n", "\n", " `['band', 'species', 'beak length (mm)', 'beak depth (mm)', 'year']`\n", "\n", "4. Concatenate the data frames into a single data frame. Be careful with indices! If you use `pd.concat()`, you will need to use the `ignore_index=True` kwarg. You might also need to use the `axis` kwarg." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**c)** The `band` field gives the number of the band on the bird's leg that was used to tag it. Are some birds counted twice? Are they counted twice in the same year? Do you think you should drop duplicate birds from the same year? How about different years? My opinion is that you should drop duplicate birds from the same year and keep the others, but I would be open to discussion on that. To practice your Pandas skills, though, let's delete only duplicate birds from the same year from the data frame. When you have made this data frame, save it as a CSV file.\n", "\n", "*Hint*: The data frame methods `duplicated()` and `drop_duplicates()` will be useful.\n", "\n", "After doing this work, it is worth saving your tidy data frame in a CSV document. To this using the `to_csv()` method of your data frame. Since the indices are uninformative, you should use the `index=False` kwarg. (I have already done this and saved it as `~/git/bootcamp/data/grant_complete.csv`, which will help you do the rest of the exercise if you have problems with this part.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**d)** Make a plots exploring how beak depth changes over time for each species. Think about what might be effective ways to display the data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**e)** It is informative to plot the measurement of each bird's beak as a point in the beak depth-beak length plane. For the 1987 data, plot beak depth vs. beak width for *Geospiza fortis* and for *Geospiza scandens*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**f)** Do part (d) again for all years. _Hint_: To display all of the plots, check out the [Bokeh documentation for layouts](https://bokeh.pydata.org/en/latest/docs/user_guide/layout.html). In your plots, make sure all plots have the same range on the axes. If you want to set two plots, say `p1` and `p2` to have the same axis ranges, you can do the following.\n", "\n", "```python\n", "p1.x_range = p2.x_range\n", "p1.y_range = p2.y_range\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 4 }