Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_01/code/solution-code/Code_1.ipynb
1904 views
Kernel: Python 3

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-7fe2d780de59> 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")
<matplotlib.collections.PathCollection at 0x925de80>
Image in a Jupyter notebook

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()
C:\Users\ystrano\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:3: FutureWarning: how in .resample() is deprecated the new syntax is .resample(...)..apply(<func>) This is separate from the ipykernel package so we can avoid doing imports until
<matplotlib.axes._subplots.AxesSubplot at 0x9428da0>
Image in a Jupyter notebook

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)
0.5
#### If you did not get a float (decimals) alter your equation to get the desired result (0.5) A = 10.0/20 print(A)
0.5

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)
2.0 0.5

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)
['the', 'cow', 'jumped', 'over', 'the', 'moon']

Question 4. How many words are in my_string?

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

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

length_of_each_word = [len(x) for x in words] print(length_of_each_word)
[3, 3, 6, 4, 3, 4]

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)
the cow jumped over the moon

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)
the||cow||jumped||over||the||moon