0% found this document useful (0 votes)
57 views26 pages

Chapter1 - BI

This document introduces data science in Python. It discusses loading and plotting data from a spreadsheet using Pandas and Matplotlib. Modules are introduced as a way to group related tools together. Common modules like Pandas, Matplotlib and Scikit-learn are presented. The document then covers importing modules, creating variables, variable types like floats and strings, displaying variables, functions, and the anatomy of functions. It provides examples of using functions like read_csv() and plot() and discusses common function errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views26 pages

Chapter1 - BI

This document introduces data science in Python. It discusses loading and plotting data from a spreadsheet using Pandas and Matplotlib. Modules are introduced as a way to group related tools together. Common modules like Pandas, Matplotlib and Scikit-learn are presented. The document then covers importing modules, creating variables, variable types like floats and strings, displaying variables, functions, and the anatomy of functions. It provides examples of using functions like read_csv() and plot() and discusses common function errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Dive into Python

I N T R O D U C T I O N T O 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 T O 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 T O 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 le er No spaces or special
(usually lowercase) characters

A er rst le er, can use Case sensitive ( my_var is


le ers/numbers/underscores di erent 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 le ers, 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'
^

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 T O 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 T O 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
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv('letter_frequency.csv')

plt.plot(df.letter_index, df.frequency, label='Ransom')


plt.show()

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 ma ers!

INTRODUCTION TO DATA SCIENCE IN PYTHON


Anatomy of a function: keyword arguments

Keyword Arguments:

Must come a er 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 T O D ATA S C I E N C E I N P Y T H O N

You might also like