Become a Python Data Analyst

5 (1 reviews total)
By Alvaro Fuentes
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

Python is one of the most common and popular languages preferred by leading data analysts and statisticians for working with massive datasets and complex data visualizations.

Become a Python Data Analyst introduces Python’s most essential tools and libraries necessary to work with the data analysis process, right from preparing data to performing simple statistical analyses and creating meaningful data visualizations.

In this book, we will cover Python libraries such as NumPy, pandas, matplotlib, seaborn, SciPy, and scikit-learn, and apply them in practical data analysis and statistics examples. As you make your way through the chapters, you will learn to efficiently use the Jupyter Notebook to operate and manipulate data using NumPy and the pandas library. In the concluding chapters, you will gain experience in building simple predictive models and carrying out statistical computation and analysis using rich Python tools and proven data analysis techniques.

By the end of this book, you will have hands-on experience performing data analysis with Python.

Publication date:
August 2018
Publisher
Packt
Pages
178
ISBN
9781789531701

 

The Anaconda Distribution and Jupyter Notebook

In this book, you will learn the basic concepts of data analysis using Python. In the first chapter, we will learn how to install the Anaconda distribution, which contains all the software needed for this book. We will also get to know Jupyter Notebook, which is the computing environment where we will do all of our work. Something nice about this book is that we take a hands-on practical approach that will help you to master the tools very effectively.

The following are the main topics that will be covered in this chapter:

  • The Anaconda distribution and the problems it solve
  • How to install it in our computer and get ready to start working
  • Jupyter Notebook, where we will perform our computations and analysis
  • Some useful commands and keyboard shortcuts used in Jupyter Notebook
 

The Anaconda distribution

Anaconda is a free, easy-to-install package management and environment management Python distribution created for developers and data scientists to make package management and deployment in scientific computing, data science, statistical analysis, and machine learning an easy task. It is software that is produced and distributed by Continuum Analytics and is available for free download at https://www.anaconda.com/download/.

Anaconda is a toolbox, a ready-to-use collection of related tools for doing data analytics with Python. The individual tools are also available for free download, but it is definitely more convenient to get the whole toolbox. This is the main problem that Anaconda solves, saving you the time it takes to look for every individual tool and install them on your system. In addition, Anaconda also takes care of package dependencies and other potential conflicts and problems that are the outcome of installing Python packages individually.

Installing Anaconda

On opening the previously mentioned URL, we are given download options for each operating system. So, here, you must look for the appropriate installer for your operating system. You will see two installers: one is for Python 3.6 and the other is for Python 2.7. In this book, we will be using Python 3.6:

Let's download the latest version of the Anaconda software and save in our Downloads folder.

Here, we choose the 64-bit installer of Anaconda 5.2 for Windows, since we will be working on a Windows environment in this book. Installation for macOS and Linux will also be similar.

Installing Anaconda is very simple; it is no different from any other software you have installed before. Double-click on the .exe file and install the software on your system. The steps are easy and you'll get the normal prompts you see when installing software:

  1. Click on Next in the first installer dialog box.
  2. Then, we have the license agreement, where we can click on I Agree after going through the terms and conditions of the software.
  3. From the options, select Just Me and click Next.
  4. Select the default installation destination folder and click Next.
  5. Next, it asks you for the environment variables and whether you want to register Anaconda as your default Python. Check both boxes and click on Install.
  6. Once the installation is complete, click on Finish in the installer dialog box.
 

Jupyter Notebook

Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualizations, and explanatory text. It is the application we will be working with in this book. Its uses include data cleaning and transformation, numerical simulation, statistical modeling, machine learning, and much more. Jupyter Notebook is similar to a canvas, or an environment, that allows you to use a programming language, in our case Python, to perform computations and to show the results in a very convenient way.

It is very convenient if you're doing some kind of analytical work, because often you want to include explanatory text, the code that produced the results, and the visualizations, which are shown in Jupyter Notebook. So it is a very convenient way of doing analytical work with any programming language, especially with Python. The Jupyter project was born out of the IPython project in 2014. Now, it has evolved to support interactive data science and scientific computing across many other programming languages, so you can use the Jupyter Notebook with many other programming languages (up to almost 20 languages). The name Jupyter comes from Julia, Python, and R, which were the three programming languages that were first supported.

Creating your own Jupyter Notebook

To start Anaconda and open a Jupyter Notebook, we click Anaconda Prompt from our list of installed programs. Anaconda Prompt is a Terminal where you can type commands. But first, let's create a folder called PythonDataScience on our desktop. This will be the directory where all the Python code that we code and run for this book in Jupyter Notebook will be stored.

Once the Terminal is open, navigate to PythonDataScience by typing the command cd Desktop/PythonDataScience and pressing Enter. To start the Jupyter Notebook application inside this directory, type the command jupyter notebook and hit Enter. This will start the application and you will see the main screen of the application opened in a tab on your browser:

You have three tabs here. One is the Files tab, where you will see all the files that you have inside the folder where you started the application. On the Running tab, you will see the things that you have running, either Terminals or Notebooks. The Clusters tab displays details about parallel computing, but we will not be using this feature in this book.

The main tab that we will be using in this book is the Files tab. To create a new Jupyter Notebook, go to New|Python 3 Notebook:

This will start a new file, which is the Jupyter Notebook where you can start coding and running your Python code.

Notebook user interfaces

Jupyter Notebook has some very useful user interfaces that display important information and notifications while you work on the Notebook. Let's go to Help and click on the first option, User Interface Tour, to take a quick look and get familiar with the interfaces of Jupyter Notebook:

The following are the main interfaces you will find on the main page of the Notebook:

  • Title (1): This is your filename and you can also change the filename of your Notebook.
  • Menu bar (2): Similar to any other desktop application, you have a menu bar where you can find different actions to do with the Notebook.
  • Toolbar (3): This is located below the menu bar and contains little icons to perform some commonly performed actions, such as saving a file, cutting cells, pasting cells, moving cells, and so on.
  • Mode indicator (4): This is located on the right-hand side of the menu bar. The Notebook has two modes, Edit mode and Command mode. Command mode has many keyboard shortcuts that you can use. In this mode, no icon is displayed in the indicator area, and the actions you can perform in this mode are actions that have to do with the file itself, such as saving the file, copying and pasting cells, and so on. Edit mode allows you to write code or text in a cell, if you are in a cell. When you are in Edit mode, you will see a little pencil in the indicator area.
A Jupyter Notebook is composed of two types of cells, code cells and text cells. The border of a selected cell will be green when you are in Edit mode. To go back to Command mode from Edit mode, you can press the Esc key or Ctrl + M. There are many keyboard shortcuts that you can use and there is a list in the Help menu.
  • Kernel indicator (5): This displays the status of the system's computation progress. To interrupt the computations that are in progress, you can use the stop button located on the toolbar.
  • Notification area (6): This area will display messages such as saving the file, or interrupting the kernel, and so on, and you will see the actions that you're performing in the notification area.
 

Using the Jupyter Notebook

Lets open a new Jupyter Notebook and create a new Python 3 Notebook, then name the Notebook as FirstPractice. As mentioned earlier, the Notebook is made out of cells, and you have two types of cells: the default cell type, which is called the code cell, and the other type of cells are text cells. We have a code cell every time we open a Notebook where you can execute any Python statement.

Running code in a code cell

We will run a few simple code statements and learn how to run those statements, and also learn how to change a cell type from code to text and vice versa. Let's execute the first code by typing 1 + 1 in the first code cell, and if you run the code of the cell with the run cell button, you will see the following output in the line below the code cell:

Next, let's create a variable, a, assign its value as 10, and run the code. Now this variable has been created, but since we didn’t create any code to compute the variable, we won’t see any output. But the statement was run, and now, if you use this variable, add 1 to it, and run the code, you will see the following result:

Now, let's see an example of the for syntax using a variable, i:

The code will tell the Notebook to print the value of i if and when the value is within the range of 10, which gives the preceding result.

Running markdown syntax in a text cell

As mentioned, the default type of each cell is a code cell, where we write Python expressions. The other type of cell that we have is a text cell, and a text cell is used to actually write text. In the cell below the output, let's type This is regular text. To tell the Notebook that this is not Python code and this is actually some text, you go to Cell | Cell Type | Markdown. Let’s run this now and you will find that what you get as output is just the text, the same text we entered:

Jupyter also allows us to format the text in many ways by using markdown syntax. If you are not familiar with markdown, you can go to Help|Markdown, where you will be taken to one of the GitHub help pages.

The markdown that you can use in Jupyter is the same markdown you use in GitHub.

There are many ways you can style and format your text; you can find all the information at: https://help.github.com/articles/basic-writing-and-formatting-syntax/. For this chapter, we will just look at the headings that are very important.

To create a heading, we add one to six # symbols before the heading text. The number of # symbols determines the size of the heading, starting from one to six # symbols, the largest to smallest heading, as follows:

# The largest heading
## The second largest heading
###### The smallest heading

The following screenshot shows the output for the preceding syntax:

If you run them in a code cell, you will get a bunch of Python commands, but we know that we want to see these as formatted text, so we need to tell the Notebook that these are actually texts by marking the cell type as markdown, and when you run the cell you get the preceding result.

Styles and formats

You can introduce other formats, such as bold, italic, strikethrough, and bold and italic. The following table shows the different styles and their corresponding syntax with an example of each:

You can also introduce quotes, for which the syntax is denoted by, the > symbol. Run a markdown cell with the following syntax:

In the words of Abraham Lincoln:
> Pardon my French

This will give the following result:

We find the style and format of the texts remains the same, similar to the line of text preceding the >, but the quoted text is a little indented from the normal indentation.

Lists

You can use or * before one or more lines to create a bulleted list. You can also use 1, 2, 3, and so on, to create a numbered or ordered list:

- George Washington
- John Adams
- Thomas Jefferson

1. James Madison
2. James Monroe
3. John Quincy Adams

On running the markdown syntax, we get the following result:

You will get a lot more styling and formatting syntax from a website that you can use in the Notebook.

Useful keyboard shortcuts

It is very annoying to use the mouse every time you want to run the cell or to transform a code cell into a markdown cell. To ease these tasks, we have a lot of keyboard shortcuts that you can use in Jupyter. Let's look at some of the most important ones.

To run a cell, for example, if you want to run the computation of 1 + 2, you can hit Alt + Enter. This shortcut will run the code in the cell and create a new cell below the output line. But if you just want to run the computation, you will press Ctrl + Enter; this will get the Notebook to run the code in that cell and show you the output, but no new cell will be created. If you want to insert a new cell below the current cell, you can press B, and then pressing A will create new cells above the current cell. This is very useful when you're working interactively within the Notebook and you want to create many cells to have more room to work.

If you find yourself using a command very often and you want to learn the shortcut key; you can go to Help|Keyboard Shortcuts. You will see a list of all the keyboard shortcuts that you can use in the Notebook.

Another very useful thing is how to transform cells from code cells to markdown cells. If you want to transform a code cell into a text cell, press the M key; this will get you from Edit mode to Command mode. Also, if you are in Command mode and you want to transform the cell into a code cell or Edit mode, press the Esc key.

 

Summary

In this chapter, we learned about the Anaconda distribution. We also installed all the software that we will use in the course, which is contained in the Anaconda distribution, and we learned about Jupyter Notebook and the basics of working with Jupyter Notebooks; about the code cells, the markdown cells, and the most useful keyboard command shortcuts to work with Jupyter and make our workflow more efficient.

In the next chapter, we will learn about NumPy, which is the core library for doing numerical computing and is at the core of every scientific computing project in the PythonX system.

About the Author

  • Alvaro Fuentes

    Alvaro Fuentes is a senior data scientist with a background in applied mathematics and economics. He has more than 14 years of experience in various analytical roles and is an analytics consultant at one of the ‘Big Three’ global management consulting firms, leading advanced analytics projects in different industries like banking, technology, and consumer goods. Alvaro is also an author and trainer in analytics and data science and has published courses and books, such as 'Become a Python Data Analyst' and 'Hands-On Predictive Analytics with Python'. He has also taught data science and related topics to thousands of students both on-site and online through different platforms such as Springboard, Simplilearn, Udemy, and BSG Institute, among others.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Great book to learn the basic stuff
Become a Python Data Analyst
Unlock this book and the full library FREE for 7 days
Start now