Editing Community Data Science Course (Spring 2016)/Day 4 Notes

From CommunityData

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 9: Line 9:
* Explore the data: find missing values, identify categorical, numerical, ordinal data fields
* Explore the data: find missing values, identify categorical, numerical, ordinal data fields
* Transform (filter, project)
* Transform (filter, project)
* Analyze
* Analyze (see todo for prompts)


# Find data. Let's start at [https://data.seattle.gov Seattle Data].
# Find data. Let's start at [https://data.seattle.gov Seattle Data].
Line 16: Line 16:
# Write exploratory scripts
# Write exploratory scripts
## Using <code>open</code> to open a file in python.
## Using <code>open</code> to open a file in python.
## In groups, explore one of these questions by building a histogram with a python dictionary:
# Write transformation script
### What kinds of values occur in <code>COLLISSIONTYPE</code>?
# In groups, answer the todo prompts.
### What kinds of values occur in <code>ADDRTYPE</code>?
### What kinds of values occur in <code>JUNCTIONTYPE</code>?
### What kinds of values occur in <code>SDOT_COLDESC</code>?
### What kinds of values occur in <code>WEATHER</code>?
### What kinds of values occur in <code>SEVERITYDESC</code>?
### (Challenge) Make a histogram of collisions by day in the data. Notice anything odd?
# Write transformation script and make a conclusion. You can work in groups. Example conclusions:
## Are incidents involving pedestrians or cyclists more likely to result in fatalities?
## Are incidents more likely to occur on rainy or wet conditions?
 
 
 
'''Code to open a file'''
 
file_handle = open('sdot_collisions_seattle.csv', 'r')  # open the csv file
for line in file_handle:                                # loop through the file one line at a time.
    line_clean = line.strip()                            # remove the newline character at end of line
    line_clean_list = line_clean.split(',')              # split the line into parts using split
    print(line_clean_list[0])                            # print the first column of data for this row.
 
 
'''Code to open a file, select a subset of rows and columns, and write to a new file'''
 
file_handle = open('sdot_collisions_seattle.csv', 'r')  # open the csv file
header = file_handle.readline()
output_handle = open('sdot_collisitions_transformed.csv', 'w')    # NOTE this will overwrite
for line in file_handle:                                # loop through the file one line at a time.
    line_clean = line.strip()                            # remove the newline character at end of line
    line_clean_list = line_clean.split(',')              # split the line into parts using split
    if int(line_clean[8]) > 0:                          # If the integer value in columns 8 is greater than one then...
        output_handle.write(line)                        # write that line to the output.
 
output_handle.close()                                    # Close the output file after the loop.
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)