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.DataFrame
methods to do preliminary analysis
Setup
General directions
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_occurrences
CSV 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 'the URL goes here') pd.read_csv(
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.csv
usingpd.read_csv()
. Store the dataframe to a variable calledprey
like this:
# Load data
= pd.read_csv('the URL goes here') prey
- What is the type of the
prey
variable? 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
prey
in 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
pandas
option. Run the following command and then look at theprey
data again:
"display.max.columns", None) pd.set_option(
- 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:
shape
columns
dtypes
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
.
check git status -> stage changes -> check git status -> commit with message -> pull -> push changes