Sign inGet started
← Back to all guides

How to use Pandas in Jupyter notebooks

By Nick Barth

Updated on March 6, 2024

Pandas is an open-source data analysis and manipulation library for Python, widely used in data science and analytics. It offers data structures and operations for manipulating numerical tables and time series, making data cleaning and analysis fast and easy. Combining Pandas with Jupyter notebooks enhances the clarity and interactivity of data exploration and analysis. In this blog post, we'll walk through some basics to get you started with using Pandas in Jupyter notebooks.

Installing Pandas in Jupyter notebook

Before you begin, ensure you have Python installed on your system. Python 3.x versions are recommended. You can download Python from the official site: python.org.

To install Pandas and Jupyter, open your command-line or terminal and run:

pip install pandas jupyter

This will install both Pandas and Jupyter notebooks in your environment.

Starting Jupyter notebook

To start Jupyter notebook:

  1. Navigate to your project directory in the terminal.
  2. Type `jupyter notebook` and press enter.
  3. Your default web browser will automatically open a new tab displaying the Jupyter Notebook dashboard.

Creating a new notebook

From the Jupyter Notebook dashboard:

  1. Click on 'New' at the top-right corner.
  2. Select 'Python 3' under the 'Notebooks' section.

A new notebook will open where you can start writing Python code.

Importing Pandas

At the top of your notebook, import Pandas and check its version as follows:

import pandas as pd

Check pandas version

print(pd.version)

It's a good practice to check the installed version for documentation purposes or when you need help troubleshooting.

Loading data

Pandas makes it straightforward to load data from various sources. For this example, we'll load a CSV file into a DataFrame. Replace `'your_data.csv'` with the path to your CSV file.

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

Display the first 5 rows of the DataFrame

df.head()

`df.head()` displays the first five rows of your dataset, providing a quick snapshot of your data.

Basic data operations

Here are a few operations that you might frequently perform on your data.

Selecting columns

To select a single column, use:

df['column_name']

For multiple columns, use a list of column names:

df[['column_name1', 'column_name2']]

Filtering rows

You can filter rows based on a condition. For example:

filtered_data = df[df['column_name'] > value]

This will return rows where the data in `'column_name'` is greater than `value`.

Applying functions

Pandas allows you to apply functions to your data easily. For instance, to calculate the mean of a column:

```python

mean_value = df['column_name'].mean()

print(mean_value)

```

Visualizing data

With Pandas, you can also quickly plot data directly from your DataFrame using Matplotlib. First, you need to import Matplotlib:

import matplotlib.pyplot as plt

Then, to plot a simple line chart:

df['column_name'].plot()
⁠
⁠plt.show()

This command plots the values from `'column_name'` and displays the chart.

Conclusion

This blog provides a basic introduction to using Pandas in Jupyter notebooks for data analysis. Pandas, combined with the interactive Jupyter notebook environment, offers a powerful toolkit for data scientists to analyze and visualize data efficiently. The simplicity with which you can perform complex operations on large datasets is one of the many reasons Pandas is a cornerstone of data science with Python. Explore the Pandas documentation and experiment with its vast array of functionalities to discover its full potential in your data analysis projects.

Nick Barth

Product Engineer

Nick has been interested in data science ever since he recorded all his poops in spreadsheet, and found that on average, he pooped 1.41 times per day. When he isn't coding, or writing content, he spends his time enjoying various leisurely pursuits.

Follow Nick on LinkedIn and GitHub

That’s it, time to try Deepnote

Get started – it’s free
Book a demo

Footer

Solutions

  • Notebook
  • Data apps
  • Machine learning
  • Data teams

Product

Company

Comparisons

Resources

  • Privacy
  • Terms

© Deepnote