Finding certain rows in a DataFrame
Bird strikes by aircraft are both dangerous to human life and of great concern to conservation efforts. Luckily, the Federal Aviation Administration has been tracking wildlife strikes for nearly 30 years.
You're going to use for loops to explore the FAA Wildlife Strikes database. A pandas DataFrame that contains wildlife strikes by aircraft in California recorded from 1990 to 2018 is loaded into your session. You want to find incidences that involve Snowy egrets, a migratory bird that winters in California. For each incident, display:
- Date of incident (
INCIDENT_DATE
column) - Airport (
AIRPORT
column) - Type of aircraft (
ATYPE
column) - Severity of the damage (
DAMAGE
column)
This exercise is part of the course
Python for MATLAB Users
Exercise instructions
- Print the first five lines of the
wildlife_strikes
DataFrame. - Construct a for loop that iterates through each
row
ofwildlife_strikes
. - Create a conditional statement that determines if
SPECIES
is a'Snowy egret'
. - If your if statement evaluates to
True
, print the'INCIDENT_DATE'
,'AIRPORT'
,' ATYPE'
, and'DAMAGE'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print the first few lines of the wildlife_strikes DataFrame
print(____)
# Loop through each row in wildlife_strikes
for i, ____ in ____.iterrows():
# Use an if statement to check the species of this row
if row['____']=='____':
# Print the INCIDENT_DATE, AIRPORT, ATYPE, and DAMAGE
print(row['____'],
____['AIRPORT'],
____['____'],
____['____'])