Learn Data
Analysis with
Python
Lessons in Coding
—
A.J. Henley
Dave Wolf
Learn Data Analysis
with Python
Lessons in Coding
A.J. Henley
Dave Wolf
Learn Data Analysis with Python: Lessons in Coding
ISBN-13 (pbk): XXXXXXXXXXISBN-13 (electronic): XXXXXXXXXX
https:
doi.org/10.1007/ XXXXXXXXXX
Li
ary of Congress Control Number: XXXXXXXXXX
Copyright © 2018 by A.J. Henley and Dave Wolf
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation,
oadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occu
ence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
esponsibility for any e
ors or omissions that may be made. The publisher makes no wa
anty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spah
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalama
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233
Spring Street, 6th Floor, New York, NY XXXXXXXXXXPhone 1-800-SPRINGER, fax XXXXXXXXXX, email
XXXXXXXXXX, or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please email XXXXXXXXXX; for reprint, pape
ack, or
audio rights, please email XXXXXXXXXX.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http:
www.apress.com
ulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub via the book’s product page, located at www.apress.com/ XXXXXXXXXX.
For more detailed information, please visit http:
www.apress.com/source-code.
Printed on acid-free pape
A.J. Henley
Washington, D.C.,
District of Columbia,
USA
Dave Wolf
Adamstown,
Maryland,
USA
https:
doi.org/10.1007/ XXXXXXXXXX
iii
About the Authors �������������������������������������������������������������������������������vii
About the Technical Reviewer �������������������������������������������������������������ix
Table of Contents
Chapter 1: How to Use This Book����������������������������������������������������������1
Installing Jupyter Notebook ����������������������������������������������������������������������������������1
What Is Jupyter Notebook? �����������������������������������������������������������������������������2
What Is Anaconda? ������������������������������������������������������������������������������������������2
Getting Started ������������������������������������������������������������������������������������������������3
Getting the Datasets for the Workbook’s Exercises �����������������������������������������4
Chapter 2: Getting Data Into and Out of Python ������������������������������������5
Loading Data from CSV Files ���������������������������������������������������������������������������������5
Your Turn ����������������������������������������������������������������������������������������������������������7
Saving Data to CSV �����������������������������������������������������������������������������������������������7
Your Turn ����������������������������������������������������������������������������������������������������������8
Loading Data from Excel Files�������������������������������������������������������������������������������8
Your Turn ����������������������������������������������������������������������������������������������������������9
Saving Data to Excel Files �����������������������������������������������������������������������������������10
Your Turn ��������������������������������������������������������������������������������������������������������11
Combining Data from Multiple Excel Files ����������������������������������������������������������11
Your Turn ��������������������������������������������������������������������������������������������������������13
Loading Data from SQL ���������������������������������������������������������������������������������������13
Your Turn ��������������������������������������������������������������������������������������������������������14
iv
Saving Data to SQL ���������������������������������������������������������������������������������������������15
Your Turn ��������������������������������������������������������������������������������������������������������16
Random Numbers and Creating Random Data ���������������������������������������������������16
Your Turn ��������������������������������������������������������������������������������������������������������18
Chapter 3: Preparing Data Is Half the Battle���������������������������������������19
Cleaning Data ������������������������������������������������������������������������������������������������������19
Calculating and Removing Outliers ���������������������������������������������������������������20
Missing Data in Pandas Dataframes ��������������������������������������������������������������22
Filtering Inappropriate Values ������������������������������������������������������������������������24
Finding Duplicate Rows ���������������������������������������������������������������������������������26
Removing Punctuation from Column Contents ����������������������������������������������27
Removing Whitespace from Column Contents ����������������������������������������������28
Standardizing Dates ��������������������������������������������������������������������������������������29
Standardizing Text like SSNs, Phone Numbers, and Zip Codes ���������������������31
Creating New Variables ���������������������������������������������������������������������������������������32
Binning Data ��������������������������������������������������������������������������������������������������33
Applying Functions to Groups, Bins, and Columns ����������������������������������������35
Ranking Rows of Data �����������������������������������������������������������������������������������37
Create a Column Based on a Conditional ������������������������������������������������������38
Making New Columns Using Functions ���������������������������������������������������������39
Converting String Categories to Numeric Variables���������������������������������������40
Organizing the Data ��������������������������������������������������������������������������������������������42
Removing and Adding Columns ���������������������������������������������������������������������42
Selecting Columns �����������������������������������������������������������������������������������������44
Change Column Name �����������������������������������������������������������������������������������45
Setting Column Names to Lower Case ����������������������������������������������������������47
Finding Matching Rows ���������������������������������������������������������������������������������48
Filter Rows Based on Conditions �������������������������������������������������������������������50
Table of ConTenTsTable of ConTenTs
v
Selecting Rows Based on Conditions ������������������������������������������������������������51
Random Sampling Dataframe �����������������������������������������������������������������������52
Chapter 4: Finding the Meaning ���������������������������������������������������������55
Computing Aggregate Statistics ��������������������������������������������������������������������������55
Your Turn ��������������������������������������������������������������������������������������������������������57
Computing Aggregate Statistics on Matching Rows �������������������������������������������58
Your Turn ��������������������������������������������������������������������������������������������������������59
Sorting Data ��������������������������������������������������������������������������������������������������������59
Your Turn ��������������������������������������������������������������������������������������������������������60
Co
elation ����������������������������������������������������������������������������������������������������������60
Your Turn ��������������������������������������������������������������������������������������������������������62
Regression ����������������������������������������������������������������������������������������������������������62
Your Turn ��������������������������������������������������������������������������������������������������������63
Regression without Intercept ������������������������������������������������������������������������������64
Your Turn ��������������������������������������������������������������������������������������������������������64
Basic Pivot Table �������������������������������������������������������������������������������������������������65
Your Turn ��������������������������������������������������������������������������������������������������������68
Chapter 5: Visualizing Data ����������������������������������������������������������������69
Data Quality Report ���������������������������������������������������������������������������������������������69
Your Turn ��������������������������������������������������������������������������������������������������������71
Graph a Dataset: Line Plot �����������������������������������������������������������������������������������71
Your Turn ��������������������������������������������������������������������������������������������������������74
Graph a Dataset: Bar Plot ������������������������������������������������������������������������������������74
Your Turn ��������������������������������������������������������������������������������������������������������76
Graph a Dataset: Box Plot �����������������������������������������������������������������������������������76
Your Turn ��������������������������������������������������������������������������������������������������������79
Table of ConTenTsTable of ConTenTs
vi
Graph a Dataset: Histogram ��������������������������������������������������������������������������������79
Your Turn ��������������������������������������������������������������������������������������������������������82
Graph a Dataset: Pie Chart ����������������������������������������������������������������������������������82
Your Turn ��������������������������������������������������������������������������������������������������������86
Graph a Dataset: Scatter Plot ������������������������������������������������������������������������������86
Your Turn ��������������������������������������������������������������������������������������������������������87
Chapter 6: Practice Problems �������������������������������������������������������������89
Analysis Exercise 1 ���������������������������������������������������������������������������������������������89
Analysis Exercise 2 ���������������������������������������������������������������������������������������������90
Analysis Exercise 3 ���������������������������������������������������������������������������������������������90
Analysis Exercise 4 ���������������������������������������������������������������������������������������������91
Analysis Project ���������������������������������������������������������������������������������������������91
Required Deliverables �����������������������������������������������������������������������������������93
Index ���������������������������������������������������������������������������������������������������95
Table of ConTenTsTable of ConTenTs
vii
About the Authors
A.J. Henley is a technology educator with over
20 years’ experience as a developer, designer,
and systems engineer. He is an instructor at
oth Howard University and Montgomery
College.
Dave Wolf is a certified Project Management
Professional (PMP) with over 20 years’
experience as a software developer, analyst,
and trainer. His latest projects include
collaboratively developing training materials
and programming bootcamps for Java and
Python.
ix
About the Technical Reviewe
Michael Thomas has worked in software
development for more than 20 years as an
individual contributor, team lead, program
manager, and vice president of engineering.
Michael has more than ten years of experience
working with mobile devices. His cu
ent focus
is in the medical sector, using mobile devices
to accelerate information transfer between
patients and health-care providers.
1© A.J. Henley and Dave Wolf 2018
A.J. Henley and D. Wolf, Learn Data Analysis with Python,
https:
doi.org/10.1007/ XXXXXXXXXX0_1
CHAPTER 1
How to Use This Book
If you are already using Python for data analysis, just
owse this book’s
table of contents. You will probably find a bunch of things that you wish
you knew how to do in Python. If so, feel free to turn directly to that chapter
and get to work. Each lesson is, as much as possible, self-contained.
Be warned! This book is more a workbook than a textbook.
If you aren’t using Python for data analysis, begin at the beginning. If
you work your way through the whole workbook, you should have a better
of idea of how to use Python for data analysis when you are done.
If you know nothing at all about data analysis, this workbook might not
e the place to start. However, give it a try and see how it works for you.
Installing Jupyter Notebook
The fastest way to install and use Python is to do what you already know
how to do, and you know how to use your
owser. Why not use Jupyter
Notebook?
2
What Is Jupyter Notebook?
Jupyter Notebook is an interactive Python shell that runs in your
owser.
When installed through Anaconda, it is easy to quickly set up a Python
development environment. Since it’s easy to set up and easy to run, it will
e easy to learn Python.
Jupyter Notebook turns your
owser into a Python development
environment. The only thing you have to install is Anaconda. In
essence, it allows you to enter a few lines of Python code, press
CTRL+Enter, and execute the code. You enter the code in cells and
then run the cu
ently selected cell. There are also options to run all
the cells in your notebook. This is useful if you are developing a larger
program.
What Is Anaconda?
Anaconda is the easiest way to ensure that you don’t spend all day
installing Jupyter. Simply download the Anaconda package and run the
installer. The Anaconda software package contains everything you need
to create a Python development environment. Anaconda comes in two
versions—one for Python 2.7 and one for Python 3.x. For the purposes of
this guide, install the one for Python 2.7.
Anaconda is an open source data-science platform. It contains over
100 packages for use with Python, R, and Scala. You can download and
install Anaconda quickly with minimal effort. Once installed, you can
update the packages or Python version or create environments for different
projects.
ChapTer 1 how To Use This Book
3
Getting Started
1. Download and install Anaconda at https:
www.
anaconda.com/download.
2. Once you’ve installed Anaconda, you’re ready to
create your first notebook. Run the Jupyter Notebook
application that was installed as part of Anaconda.
3. Your
owser will open to the following address:
http:
localhost:8888. If you’re running
Internet Explorer, close it. Use Firefox or Chrome
for best results. From there,
owse to http:
localhost:8888.
4. Start a new notebook. On the right-hand side of the
owser, click the drop-down button that says "New"
and select Python or Python 2.
5. This will open a new iPython notebook in another
owser tab. You can have many notebooks open in
many tabs.
6. Jupyter Notebook contains cells. You can type Python
code in each cell. To get started (for Python 2.7),
type print "Hello, World!" in the first cell and
hit CTRL+Enter. If you’re using Python 3.5, then the
command is print("Hello, World!").
ChapTer 1 how To Use This Book
https:
www.anaconda.com/download
https:
www.anaconda.com/download
4
Getting the Datasets for the Workbook’s
Exercises
1. Download the dataset files from http:
www.
ajhenley.com/dwnld.
2. Upload the file datasets.zip to Anaconda in the
same folder as your notebook.
3. Run the Python code in Listing 1-1 to unzip the
datasets.
Listing 1-1. Unzipping dataset.zip
path_to_zip_file = "datasets.zip"
directory_to_extract_to = ""
import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
ChapTer 1 how To Use This Book
http:
www.ajhenley.com/dwnld
http:
www.ajhenley.com/dwnld
5© A.J. Henley and Dave Wolf 2018
A.J