AppDividend
Latest Code Tutorials

Python Jupyter Notebook Tutorial | Getting Started With Jupyter Notebook

0 69

Get real time updates directly on you device, subscribe now.

In this example, we will see Python Jupyter Notebook Tutorial With Example. Python Jupyter Notebook is an incredibly powerful tool for developing and presenting the data science projects. The Jupyter Notebook integrates code and its output into the single document that combines visualizations, narrative text, mathematical equations, and rich media. The interactive workflow promotes an iterative and rapid development, making notebooks an increasingly popular choice of contemporary data science, analysis, and increasingly science at large.

Getting Started With Jupyter Notebook

You can install Jupyter Notebook by installing Anaconda. I am using Macbook, but the procedure in windows is almost the same.

You can install using this link.

If you are on windows then, you should use this link

Anaconda is the most popula Python distribution for data science, machine learning and comes pre-loaded with all the most popular libraries and tools. 

Some of the biggest Python libraries included in Anaconda are NumPypandas, and Matplotlib, though the full 1000+ list is exhaustive. 

Anaconda lets us hit the ground running in your own fully stocked data science workshop without the hassle of managing the many installations or worrying about OS-specific dependencies.

Installation of Jupyter Notebook

The installation process is straightforward, and after you install the Anaconda, you will see the screen like below.

 

Getting Started With Jupyter Notebook

It is an Anaconda Navigator. Here, you can see the second option is a jupyter notebook, which we need to launch to work with Python. Let’s launch it, and your terminal will be opened, and it will start a jupyter notebook on browser whose local URL is: http://localhost:8888/tree.

Congratulations!! You have installed successfully.

Creating Your First Notebook

First, you need to select a project folder. I have selected mine which is in desktop/code/pyt folder. For this project, I am using Python 3. That is why you will select your python version to 3.

Now create a file whose extension will be .ipynb.

What is an ipynb File?

 Each .ipynb file is the text file that describes the contents of your notebook in the format called JSON. Each cell and its contents, including image attachments that have been converted into the strings of text, is listed there with some metadata.

Jupyter Notebook interface

I have created a Jupyter Notebook file called DataScience.ipynb. It looks like below image.

 

Python Jupyter Notebook Tutorial

In Jupyter Notebook, Cells create a body of the notebook. In the below screenshot of a new notebook that box with a green outline is the empty cell. There are mainly two main cell types that we will cover:

  • The code cell, which contains code to be executed in the kernel and displays its output below.
  • The Markdown cell, which contains the text formatted using a Markdown and displays its output in-place when it is run.

First cell in the new notebook is always the code cell. Let’s test it out with a classic hello world example. Type the following code inside the cell.

Related Posts
1 of 42
19 + 2

Now click the Run button in the toolbar above or press Ctrl + Enter. The result should look like this below.

 

Python Jupyter Notebook Tutorial With Example

The output is instantly shown in the next line. This is the beauty of Jupyter Notebook.

After, you can add, remove or edit the cells according to your requirements. Also, don’t forget to insert explanatory text or titles and subtitles to clarify your code That what makes the notebook a real notebook in the end.

Running Jupyter Notebook The Pythonic Way: Pip

If you don’t want to install Anaconda, then make sure that you have the latest version of pip. 

If you have installed Python, you will typically already have it. Now, upgrade you pip version, if you have an old one. Type the following commands concerning your operating system.

# On Windows
python -m pip install -U pip setuptools

# On OS X or Linux
pip install -U pip setuptools

Once you have pip installed on your machine, you can just run the following command.

# Python2
pip install jupyter

# Python 3
pip3 install jupyter

Now that you know what you will be working with and you have installed it, it’s time to get started for real!

Run a following command to open up the application.

jupyter notebook

Then you’ll see the application opening in a web browser on a following address: http://localhost:8888.

So, we have seen both ways to install Jupyter Notebook.

Data Analysis With Pandas and Jupyter Notebook

Now, after we have successfully installed the Jupyter Notebook, we will import the pandas library to work with the datasets. If you are new to Python Pandas library, then check out my this article.

Okay, let’s download the DataSet for our example. You need to visit the following link.

https://docs.google.com/spreadsheets/d/1zeeZQzFoHE2j_ZrqDkVJK9eF7OH1yvg75c8S-aBcxaU/edit#gid=0

It’s a data of Summer Olympic medallists 1896 to 2008. It is publicly available.

Now, open that link and save that file to data.csv inside the same project folder where the jupyter file is there. Make sure that both are in the same directory.

Okay, import that file and skip the first four rows of that file by doing the following code. The skiprows parameter indicated the Line numbers to skip (0-indexed) or some lines to skip (int) at the start of the file.

 

Data Analysis With Pandas and Jupyter Notebook

Here, we have written the three line of code, and we get the data.

import pandas as pd
olympicsData = pd.read_csv('data.csv', skiprows=4)
olympicsData.head()

If you are getting the same data then perfect, you are in the right direction, and you have successfully imported the data.

Access the DataFrames in Jupyter Notebook

Next step is to access the DataFrame from that data. Type the following code inside the notebook cell and hit the ctrl + enter.

olympicsData

You will see the first 30 rows and last 30 rows as a result.

 

Access the DataFrames in Jupyter Notebook

Access the Series in Notebook

A series is a one-dimensional array of index data.

Now, if we want to access the series from the Olympics data, then you need to pass the column name as an index and see the output. Let’s say we need to see all the Sport in the Olympics. Write the following code inside the cell.

olympicsData['Sport']

See the output below.

 

Access the Series in Notebook

So, this is how you can access the data from the csv data file and using different Python Pandas data structure; you can perform the operations on that data.

Finally, Python Jupyter Notebook Tutorial | Getting Started With Jupyter Notebook is over.

Thanks for taking this demo and will see you soon.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.