Prey species in the California drylands
Week 1 - Discussion section
This discussion section will guide you through preliminary data exploration for a real world dataset about animal observations in the California drylands. In this discussion section, you will:
- Collaborate with your new team!
- Practice version control using git via the terminal
- Obtain information about a dataset from an online data repository
- Use the
pandas.read_csv()function for loading files directly from a URL - Use
pandas.DataFramemethods to do preliminary analysis
Teamwork
General directions
Setup
About the data
For these exercises we will use data about prey items for endangered terrestrial vertebrate species within central California drylands[1] [2].
This dataset is stored in the Knowledge Network for Biocomplexity (KNB) data repository. This is an international repository intended to facilitate ecological and environmental research. It has thousands of open datasets and is hosted by the National Center for Ecological Analysis and Synthesis (NCEAS).
1. Archive exploration
When possible, data exploration should start at the data repository. Take some time to look through the dataset’s description in the KNB data repository. Discuss the following questions with your team:
- What is this data about?
- Is this data collected in-situ by the authors or is it a synthesis of multiple datasets?
- During what time frame were the observations in the dataset collected?
- Does this dataset come with an associated metadata file?
- Does the dataset contain sensitive data?
In your notebook: use a markdown cell to add a brief description of the dataset, including a citation, date of access, and a link to the archive.
check git status -> stage changes -> check git status -> commit with message -> pull -> push changes
2. Metadata exploration
You may have noticed there are two metadata files: Compiled_occurrence_records_for_prey_items_of.xml and metadata_arth_occurrences.csv. The .xml document file type is EML which stands for EML: Ecological Metadata Language. This is a machine-readable file that has metadata about the whole dataset. In this section we will only use the metadata in the CSV file.
Back in your notebook, import the pandas package using standard abbreviation in a code cell. Then follow these steps to read in the metadata CSV using the pandas.read_csv() function:
- Navigate to the data package site and copy the URL to access the
metadata_arth_occurrencesCSV file. To copy the URL:
- hover over the Download button –> right click –> “Copy Link”.
Read in the data from the URL using the
pd.read_csv()function like this:# Access metadata from repository pd.read_csv('the URL goes here')Take a minute to look at the descriptions for the columns.
Note: Not all datasets have column descriptions in a CSV file. Often they come with a .doc or .txt file with information.
3. Data loading
- Follow steps (a) and (b) from the previous exercise to read in the drylands prey data file
arth_occurrences_with_env.csvusingpd.read_csv(). Store the dataframe to a variable calledpreylike this:
# Load data
prey = pd.read_csv('the URL goes here')- What is the type of the
preyvariable? Use a Python function get this information.
check git status -> stage changes -> check git status -> commit with message -> pull -> push changes
CHECK IN WITH YOUR TEAM
MAKE SURE YOU’VE ALL SUCCESSFULLY ACCESSED THE DATA BEFORE CONTINUING
4. Look at your data
Run
preyin a cell. What do you notice in the columns section?To see all the column names in the same display we need to set a
pandasoption. Run the following command and then look at thepreydata again:
pd.set_option("display.max.columns", None)- Add a comment explaining what
pd.set_option("display.max.columns", None)does.
check git status -> stage changes -> check git status -> commit with message -> pull -> push changes
5. pd.DataFrame preliminary exploration
Run each of the following methods for prey in a different cell and write a brief description of what they do as a comment:
head()tail()info()nunique()
For example:
# head()
# returns the first five rows of the data frame
prey.head()
If you’re not sure about what the method does, try looking it up in the pandas.DataFrame documentation.
- Check the documentation for
head(). If this function has any optional parameters, change the default value to get a different output.
Print each of the following attributes of prey in a different cell and write a brief explanation of what they are as a comment:
shapecolumnsdtypes
If you’re not sure about what information is the attribute showing, look it up in the pandas.DataFrame documentation!
check git status -> stage changes -> check git status -> commit with message -> pull -> push changes
6. Update column names
Change the column names of institutionCode and datasetKey to institution_code and dataset_key, respectively. Make sure you’re actually updating the dataframe. HINT: look for the documentation on the rename method for pandas.DataFrames.
7. Subsetting
Complete the following subsetting tasks.
- Select only the preservered specimes from
prey. - Select observations between 1992 and 2000
- Select all rows for columns
speciesandeventDate. - Select observations that are greater than 37° latitude and less than 39° latitude
check git status -> stage changes -> check git status -> commit with message -> pull -> push changes