Dive into Python
I N T R O D U C T I O N TO D ATA S C I E N C E I N P Y T H O N
Hillary Green-Lerman
Lead Data Scientist, Looker
What you'll learn
How to write and execute Python code with DataCamp
How to load data from a spreadsheet
How to turn data into beautiful plots
INTRODUCTION TO DATA SCIENCE IN PYTHON
Solving a mystery with data
INTRODUCTION TO DATA SCIENCE IN PYTHON
Using the IPython shell
INTRODUCTION TO DATA SCIENCE IN PYTHON
Using the script editor
INTRODUCTION TO DATA SCIENCE IN PYTHON
What is a module?
Groups related tools together
Makes it easy to know where to look for a particular tool
Common examples:
matplotlib
pandas
scikit-learn
scipy
nltk
INTRODUCTION TO DATA SCIENCE IN PYTHON
Importing pandas and matplotlib
import pandas as pd
from matplotlib import pyplot as plt
# Pandas loads our data
df = pd.read_csv('ransom.csv')
# Matplotlib plots and displays
plt.plot(df.letters, df.frequency)
plt.show()
INTRODUCTION TO DATA SCIENCE IN PYTHON
Importing a module
Importing a Module
import pandas
Importing a module with an alias
import pandas as pd
INTRODUCTION TO DATA SCIENCE IN PYTHON
Let's practice!
I N T R O D U C T I O N TO D ATA S C I E N C E I N P Y T H O N
Creating variables
I N T R O D U C T I O N TO D ATA S C I E N C E I N P Y T H O N
Hillary Green-Lerman
Lead Data Scientist, Looker
Filing a missing puppy report
name = "Bayes"
height = 24
weight = 75.5
INTRODUCTION TO DATA SCIENCE IN PYTHON
Rules for variable names
Must start with a letter No spaces or special
(usually lowercase) characters
After rst letter, can use Case sensitive ( my_var is
letters/numbers/underscores different from MY_VAR )
# Valid Variables # Invalid Variables
bayes_weight bayes-height
b bayes!
bayes42 42bayes
INTRODUCTION TO DATA SCIENCE IN PYTHON
Error messages
bayes-height = 3
File "<stdin>", line 1
bayes-height = 3
^
SyntaxError: can't assign to operator
INTRODUCTION TO DATA SCIENCE IN PYTHON
Floats and strings
oat: represents an integer or decimal number
height = 24
weight = 75.5
string: represents text; can contain letters, numbers, spaces, and
special characters
name = 'Bayes'
breed = "Golden Retriever"
INTRODUCTION TO DATA SCIENCE IN PYTHON
Common string mistakes
Don't forget to use quotes! Without quotes, you'll get a name error.
owner = DataCamp
File "<stdin>", line 1, in <module>
owner = DataCamp
NameError: name 'DataCamp' is not defined
Use the same type of quotation mark. If you start with a single
quote, and end with a double quote, you'll get a syntax error.
fur_color = "blonde'
File "<stdin>", line 1
fur_color = "blonde'
^
SyntaxError: EOL while scanning string literal
INTRODUCTION TO DATA SCIENCE IN PYTHON
Displaying variables
name = "Bayes"
height = 24
weight = 75
print(height)
24
INTRODUCTION TO DATA SCIENCE IN PYTHON
Let's practice!
I N T R O D U C T I O N TO D ATA S C I E N C E I N P Y T H O N
What is a function?
I N T R O D U C T I O N TO D ATA S C I E N C E I N P Y T H O N
Hillary Green-Lerman
Lead Data Scientist, Looker
A function is an action
INTRODUCTION TO DATA SCIENCE IN PYTHON
Functions in code
Functions perform actions:
pd.read_csv() turns a csv le into a table in Python
plt.plot() turns data into a line plot
plt.show() displays plot in a new window
INTRODUCTION TO DATA SCIENCE IN PYTHON
INTRODUCTION TO DATA SCIENCE IN PYTHON
Anatomy of a function: function name
Function Name:
Starts with the module that the function "lives" in ( plt )
Followed by the name of the function ( plot )
Function name is always followed by parentheses ()
INTRODUCTION TO DATA SCIENCE IN PYTHON
Anatomy of a function: positional arguments
Positional Arguments:
These are inputs to a function; they tell the function how to do
its job
Order matters!
INTRODUCTION TO DATA SCIENCE IN PYTHON
Anatomy of a function: keyword arguments
Keyword Arguments:
Must come after positional arguments
Start with the name of the argument ( label ), then an equals
sign ( = )
Followed by the argument ( Ransom )
INTRODUCTION TO DATA SCIENCE IN PYTHON
Common function errors
Missing commas between arguments
Missing closed parenthesis
INTRODUCTION TO DATA SCIENCE IN PYTHON
Let's practice!
I N T R O D U C T I O N TO D ATA S C I E N C E I N P Y T H O N