Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/april_18/lessons/lesson-01/code/solution-code/solution-code-1.ipynb
1905 views
Kernel: Python 2

Check to see if you're ready to go on Thursday!

  1. Run each block of code

  2. Check for errors

  3. When you think you're error free, flag down a teaching team member to confirm!

###This is what an error looks like print(a)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-ccb7e0456e36> in <module>() 1 ###This is what an error looks like ----> 2 print(a) NameError: name 'a' is not defined

Objectives

Get comfortable with IPython Notebook

  • How to start IPython Notebook

  • How to read data into pandas

  • How to do simple manipulations on pandas dataframes

Start a notebook:

For each class, we'll be using a set of common data science libraries and tools, like the IPython notebook. You can start an IPython notebook by running

jupyter notebook $NAME_OF_FILE

Try it yourself!

Read and run the block of code below by:

  1. Clicking on it and pressing the play button above or

  2. Using a short cut- (help --> keyboard shortcuts)

%matplotlib inline import matplotlib.pyplot as plt import matplotlib as mpl import pandas as pd mpl.rcParams['figure.figsize'] = (15, 6) pd.set_option('display.width', 4000) pd.set_option('display.max_columns', 100)

First: Read in the data

Review Simple Commands

Practice downloading and reading into sample data

# Download and read the data (this may take more than 1 minute) orig_data = pd.read_csv('../../assets/dataset/311-service-requests.csv', parse_dates=['Created Date'], low_memory=False)
plt.scatter(orig_data['Longitude'], orig_data['Latitude'], marker='.', color="purple")

Try this Example:

Graph the number of noise complaints each hour in New York

complaints = orig_data[['Created Date', 'Complaint Type']] noise_complaints = complaints[complaints['Complaint Type'] == 'Noise - Street/Sidewalk'] noise_complaints.set_index('Created Date').sort_index().resample('H', how=len).plot()

Second: Using IPython

Review Python Basics

Test your skills by answering the following questions:

Question 1. Divide 10 by 20 and set the result to a variable named "A"

A = 10/20 print(A)
#### If you did not get a float (decimals) alter your equation to get the desired result (0.5) A = 10.0/20 print(A)

Question 2. Create a function called division that will divide any two numbers and prints the result (with decimals).

Call your function. Confirm that the results are as expected.

def division(numerator, denominator): result = float(numerator) / denominator print(result) division(20, 10) division(10, 20)

Question 3. Using .split() split my string into separate words

my_string = "the cow jumped over the moon" words = my_string.split() # returns ['the', 'cow', 'jumped', 'over', 'the', 'moon'] print(words)

Question 4. How many words are in my_string?

word_count = len(words) #returns the number of words- 6 print(word_count)

Question 5. Use a list comprehension to find the length of each word

length_of_each_word = [len(word) for word in words] print(length_of_each_word)

Question 6. Put the words back together in a variable called sentence using .join()

# put them back together with join sentence = " ".join(words) print(sentence)

Bonus: Add a "||" between each word

# the " " puts the space in between the words. or you could put anything else in alternate_sentence = "||".join(words) print(alternate_sentence)