Water conflicts in the Colorado River Basin

Week 2 - Discussion section

This discussion section will guide you through exploring data about water-related conflicts at the Colorado River Basin using data from the U.S. Geological Survey (USGS). In this discussion section, you will:

Setup

  1. In the workbench-1 server, start a new JupyterLab session or access an active one.

  2. In the terminal, use cd to navigate into the eds220-discussion-sections directory. Use pwd to verify eds220-discussion-sections is your current working directory.

  3. Create a new Python notebook inside your eds220-discussion-sections directory and rename it to section-2-co-basin-water-conflicts.ipynb.

  4. Use the terminal to stage, commit, and push this file to the remote repository. Remember:

    1. git status : check git status
    2. git add FILE-NAME : stage updated file
    3. git status : check git status again to confirm
    4. git commit -m "Commit message" : commit with message
    5. git pull : check local repo is up to date (best practice)
    6. git push : push changes to upstream repository

CHECK IN WITH YOUR TEAM

MAKE SURE YOU’VE ALL SUCCESSFULLY SET UP YOUR NOTEBOOKS BEFORE CONTINUING

General directions

  • Add comments in each one of your code cells.
  • Include markdown cells in between your code cells to add titles and information.
  • Indications about when to commit and push changes are included, but you are encouraged to commit and push more often.

About the data

For these exercises we will use data about Water Conflict and Crisis Events in the Colorado River Basin [1]. This dataset is stored at ScienceBase, a digital repository from the U.S. Geological Survey (USGS) created to share scientific data products and USGS resources.

The dataset is a CSV file containing conflict or crisis events around water resource management in the Colorado River Basin. The Colorado River Basin, inhabited by several Native American tribes for centuries, is a crucial water source in the southwestern United States and northern Mexico, supporting over 40 million people, extensive agricultural lands, and diverse ecosystems. Its management is vital due to the region’s arid climate and the competing demands for water, leading to significant challenges related to water allocation and conservation.

Colorado River Basin. U.S. Bureau of Reclamation.

1. Archive exploration

  1. Look through the dataset’s description in the ScienceBase repository. Find the following information:

    1. Where was the data collected from?
    2. During what time frame were the observations in the dataset collected?
    3. What was the author’s perceived value of this dataset?
  2. In a markdown cell, use your answers to the previous questions to add a brief description of the dataset. Briefly discuss anything else that seems relevant to you. Include a citation, date of access, and a link to the archive.

  3. Take a look at the data’s metadata by clicking on the “View” icon of the Coded Events Colorado River Basin Water Conflict Table Metadata.xml file.

check git status -> stage changes -> check git status -> commit with message -> pull -> push changes

2. Data loading

  1. Create a new directory data/ inside your eds220-discussion-sections directory.

  2. Download the Colorado River Basin Water Conflict Table.csv file from the Science Base repository and upload it into the data/ folder.

  3. Update the .gitignore file of your eds220-discussion-sections so it ignores the data/ folder. Verify that git is ignoring the data file.

You have at least three ways of doing this:

  • Via GitHub: This is how we previously did it in class. If you update the .gitignore file via GitHub, don’t forget to run git pull when you go back to your repository in the workbench.
  • Via VSCode or Positron: you can directly edit the .gitignore and push your changes.
  • Via the terminal: in JupyterLab .gitignore is a hidden file that’s invisible from the file navigation panel. You can edit it through the terminal.
  1. Load the data into your section-2-co-basin-water-conflicts.ipynb notebook. Name your data frame variable df.

3. Preliminary data exploration

  1. Set pandas to display all columns in the data frame.

  2. Using pandas methods, obtain preliminary information and explore this data frame in at least four different ways.

CHECK IN WITH YOUR TEAM 🙌

YOU CAN SLACK THEM TO LET THEM KNOW YOU’RE READY FOR THE SESSION OR BRING UP ANY QUESTIONS

MAKE SURE YOU’VE ALL SUCCESSFULLY LOADED THE DATA AND DONE A PRELIMINARY EXPLORATION BEFORE CONTINUING

check git status -> stage changes -> check git status -> commit with message -> pull -> push changes

4. Location column descriptions

In these exercises we will work with columns in the data frame pertaining to the location of an event. Before continuing, read the following column descriptions from the .xml metadata file:

Column Description
Place Where the event actually occurred, but also where the event’s direct implications are felt most directly. When the researchers reviewed the articles, they were looking for mentions of specific places impacted by the events. Empty cell indicates a place was not coded for this event. NA indicates a place is not referenced in the event text.
State State Name coded from Place field. Empty cell indicates a state was not coded for this event or that the article was not coded.
Conflict Present Binary response to “Is a conflict present?” Conflict is defined here as an incompatibility of goals and various attempts of any present parties to control each other’s choices. When the researchers reviewed the articles, they were looking for evidence of this behavior in the event explained.

5. String accessor for pandas.Series

In the following exercises we will work with pandas.Series whose values are strings. This is a common scenario, so pandas has special string methods for this kind of series. These methods are accessed via the str accessor. Accessors provide additional functionality for working with specific kinds of data (in this case, strings).

  1. The code below gives a brief demonstration of using the str accessor to use the split() method for pandas.Series. Carefully read the code.
# CELL 1
import numpy as np
import pandas as pd 

# Example series
s = pd.Series(['California; Nevada', 'Arizona', np.nan, 'Nevada; Utah'])
s
0    California; Nevada
1               Arizona
2                   NaN
3          Nevada; Utah
dtype: object
# CELL 2
# str accessor (doesn't do anything by itself)
s.str
<pandas.core.strings.accessor.StringMethods at 0x14fa6b190>
# CELL 3
# Use str accessor with additional methods to perform string operations
s.str.split(';')
0    [California,  Nevada]
1                [Arizona]
2                      NaN
3          [Nevada,  Utah]
dtype: object
# CELL 4
s.str.split(';').explode()
0    California
0        Nevada
1       Arizona
2           NaN
3        Nevada
3          Utah
dtype: object


  1. Within your team, form pairs:
    • Person 1 explains to person 2 what is the output of CELL 3.
    • Afterwards, person 2 explains to person 1 what is the output of CELL 4.

Check in with your team. We’ll use some of this code later on.

6. Examine state codes

Our goal today is to find which states are reported in the dataset as having water conflicts.

  1. Take a look at the non-NA values in the State column. HINT: notna() method for pandas.Series.

  2. What could be a challenge to finding the states that have reported having water conflicts (without repetition)?

7. Brainstorm

  1. Individually, write downm step-by-step instructions on how you would wrangle the data frame to obtain an array (without repetition) of the state codes in which a water conflict has been reported. Would you keep or remove NA values and why? It’s ok if you don’t know how to code each step - it’s way more important to have an idea of what you would like to do.

  2. Discuss your step-by-step instructions with your team.

The next exercises will guide you through finding these unique state codes. There are many ways of extracting this information. The one presented here might not be the same way you thought about doing it - that’s ok! This one was designed to practice using the .str accessor in a pandas.Series.

8. Exploratory wrangling

  1. Perform the following wrangling:
    1. select the State column and the rows corresponding to water conflicts occurring
    2. split the strings in the column by the delimiter ;
    3. explode the resulting pandas.Series to have each element of a list as a separate row
    4. find the unique string values in the resulting pandas.Series, keeping the NAs

CHECK IN WITH YOUR TEAM: IS EVERY STEP CLEAR?

check git status -> stage changes -> check git status -> commit with message -> pull -> push changes

9. Find unique state codes

  1. Discuss with your team: Why do some state codes seem to be repeated? What would we need to do to get the correct strings?

  2. Update your code to obtain an array of codes (without repetition) of the states mentioned in the news articles about water conflicts in the Colorado River Basin. Hint: str.strip().

Once your code works, rewrite it as a single method chain, with one method per line inside parentheses.

10. Visualize data exploration & discussion

  1. Create a bar plot showing the number of conflicts reported per state and write a description of these results. Include a bar for NA values.

  2. Using the dataset’s metadata, what are possible reasons a state might be missing? How does this affect what we can claim for our array of states?

  3. Zoom out: every dataset you will work with was created by people, instruments or models, through choices about what to record and how.

    • In this section, which data issues did you find only because you looked closely (for example, extra spaces or missing values)? What would have happened to your results if you hadn’t noticed them?

    • What are a couple of checks you could make a habit of whenever you start working with a new dataset?

END OF SESSION

check git status -> stage changes -> check git status -> commit with message -> pull -> push changes

References

[1]
D. V. Holloman, M. K. Hines, and D. K. Zoanni, “Coded Water Conflict and Crisis Events in the Colorado River Basin, Derived from LexisNexis search 2005-2021.” U.S. Geological Survey, 2023. doi: 10.5066/P9X6WR7J. Available: https://www.sciencebase.gov/catalog/item/63acac09d34e92aad3ca1480. [Accessed: Sep. 27, 2024]