Book Image

Python Business Intelligence Cookbook

Book Image

Python Business Intelligence Cookbook

Overview of this book

The amount of data produced by businesses and devices is going nowhere but up. In this scenario, the major advantage of Python is that it's a general-purpose language and gives you a lot of flexibility in data structures. Python is an excellent tool for more specialized analysis tasks, and is powered with related libraries to process data streams, to visualize datasets, and to carry out scientific calculations. Using Python for business intelligence (BI) can help you solve tricky problems in one go. Rather than spending day after day scouring Internet forums for “how-to” information, here you’ll find more than 60 recipes that take you through the entire process of creating actionable intelligence from your raw data, no matter what shape or form it’s in. Within the first 30 minutes of opening this book, you’ll learn how to use the latest in Python and NoSQL databases to glean insights from data just waiting to be exploited. We’ll begin with a quick-fire introduction to Python for BI and show you what problems Python solves. From there, we move on to working with a predefined data set to extract data as per business requirements, using the Pandas library and MongoDB as our storage engine. Next, we will analyze data and perform transformations for BI with Python. Through this, you will gather insightful data that will help you make informed decisions for your business. The final part of the book will show you the most important task of BI—visualizing data by building stunning dashboards using Matplotlib, PyTables, and iPython Notebook.
Table of Contents (12 chapters)
Python Business Intelligence Cookbook
About the Author
About the Reviewer


Data! Everyone is surrounded by it, but few know how to truly exploit it. For those who do, glory awaits!

Okay, so that's a little dramatic; however, being able to turn raw data into actionable information is a goal that every organization is working to achieve. This book helps you achieve it.

Making sense of data isn't some esoteric art requiring multiple degrees—it's a matter of knowing the recipes to take your data through each stage of the process. It all starts with asking an interesting question.

My mission is that, by the end of this book, you will be equipped to apply Python to business intelligence tasks—preparing, exploring, analyzing, visualizing, and reporting—in order to make more informed business decisions using the data at hand.

Prepare for an awesome read, my friend!

A little context first. The code in this book is developed on Mac OS X 10.11.1, using Python 3.4.3, IPython 4.0.0, matplotlib 1.4.3, NumPy 1.9.1, scikit-learn 0.16.1, and Pandas 0.16.2—in other words, the latest or near-latest versions at the time of publishing.

What this book covers

Chapter 1, Getting Set Up to Gain Business Intelligence, covers a set of installation recipes and advice on how to install the required Python packages and libraries as well as MongoDB.

Chapter 2, Making Your Data All It Can Be, provides recipes to prepare data for analysis, including importing the data into MongoDB, cleaning the data, and standardizing it.

Chapter 3, Learning What Your Data Truly Holds, shows you how to explore your data by creating a Pandas DataFrame from a MongoDB query, creating a data quality report, generating summary statistics, and creating charts.

Chapter 4, Performing Data Analysis for Non Data Analysts, provides recipes to perform statistical and predictive analysis on your data.

Chapter 5, Building a Business Intelligence Dashboard Quickly, builds on everything that you've learned and shows you how to generate reports in Excel, and build web-based business intelligence dashboards.

What you need for this book

For this book, you will need Python 3.4 or a later version installed on your operating system. This book was written using Python 3.4.3 installed by Continuum Analytics' Anaconda 2.3.0 on Mac OS X El Capitan version 10.11.1.

The other software packages that are used in this book are IPython, which is an interactive Python environment that is very powerful and flexible. This can be installed using package managers for Mac OSes or prepared installers for Windows and Linux-based OSes.

If you are new to Python installation and software installation in general, I highly recommend using the Anaconda Python distribution from Continuum Analytics.

Other required software mainly comprises Python packages that are all installed using the Python installation manager, pip, which is a part of the Anaconda distribution.

Who this book is for

This book is intended for data analysts, managers, and executives with a basic knowledge of Python who now want to use Python for their BI tasks. If you have a good knowledge and understanding of BI applications and have a working system in place, this book will enhance your toolbox.


In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Use this recipe to import the Accidents7904.csv file into MongoDB."

A block of code is set as follows:

from pymongo import MongoClient
client = MongoClient()
db = client.pythonbicookbook 
files = db.files 
f = open('name_of_file_here.txt')
text =
doc = {

Any command-line input or output is written as follows:

# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Highlight your new connection and click Connect."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.