Jupyter – Importing & Exporting Data

Your notebook is a kitchen. Data is your ingredients. Learn how to bring them in — and ship the finished dish out.

The big idea

Your notebook doesn't hold data — it just works with it

Think of your notebook like a chef

A chef doesn't grow the vegetables or bottle the olive oil. They bring ingredients in from outside, cook them, and send the meal out. Your Jupyter notebook works the same way. It doesn't store data — it just borrows it, does something with it, then sends the results somewhere.

Your data file
CSV, Excel, database...
→
Jupyter notebook
clean, analyze, transform
→
Output file
CSV, Excel, JSON...
The notebook only holds code and results. Your actual data lives in separate files — always keep your data files in the same folder as your notebook.
Import

Bring data in

Load a file so Python can read and work with it.

Export

Send data out

Save your cleaned or processed data to a new file.

Importing data

Four ways to bring data into your notebook

First: always import pandas

Almost every import method uses pandas — the go-to Python library for working with data. Think of it as the knife every chef needs before they start cooking.

import pandas as pd
The as pd part is a shortcut — instead of typing pandas every time, you write pd. Everyone does this. It's a universal convention.

Pick your import method

Click each tab to see the code and when to use it.

A CSV is a plain text file where values are separated by commas. It's the most common data format — like a spreadsheet saved as plain text.

import pandas as pd data = pd.read_csv("students.csv") data.head()
Output (first 5 rows)
name age score 0 Ada 19 85 1 Bello 21 90 2 Chisom 20 78
.head() shows just the first 5 rows — great for peeking at your data without printing thousands of rows at once.

Excel files (.xlsx) are common in offices and schools. Pandas can read them directly — no need to convert to CSV first.

data = pd.read_excel("results.xlsx") data.head()
You may need to install openpyxl first. Run this in a cell:
!pip install openpyxl

If data lives on a website (like a government open data portal), you can load it directly from its URL — no downloading needed.

url = "https://example.com/population.csv" data = pd.read_csv(url) data.head()
This is super handy for public datasets — your notebook always uses the latest version of the data every time it runs.

Databases store large amounts of structured data. You connect to one and use SQL to fetch exactly the rows you need.

import sqlite3 import pandas as pd conn = sqlite3.connect("school.db") data = pd.read_sql_query( "SELECT * FROM students", conn ) data.head()
Beginners: don't worry about databases yet — start with CSV files and come back to this later when you're comfortable.

Exporting data

Save your work so you can share it or use it elsewhere

Why export?

After cleaning or analyzing data in your notebook, you'll want to save the results. Maybe you're sending it to a colleague, uploading it to a website, or feeding it into another tool. Exporting lets you do that.

Imagine you cleaned up a messy dataset

You removed blank rows, fixed typos, and added a "grade" column. Now you want to save that clean version — not just see it inside the notebook.

data = data.dropna() data["grade"] = data["score"].apply( lambda x: "A" if x >= 80 else "B" )
Now let's save this cleaned data to a file...

Choose your export format

CSV is the safest format — almost every tool on earth can open it. Use this when in doubt.

data.to_csv("clean_results.csv", index=False)
index=False stops pandas from adding an ugly extra column of row numbers to your file. Almost always include it.

Great for sharing with people who prefer spreadsheets — keeps formatting, supports multiple sheets, etc.

data.to_excel("clean_results.xlsx", index=False)

JSON is a format used by websites and apps. Use this when you're feeding data into a web application or API.

data.to_json("clean_results.json")
.csv

Universal. Use for everything.

.xlsx

For humans who use Excel.

.json

For apps and websites.

Exporting the notebook itself

Share your whole analysis — code, results, and all

Data vs notebook — what's the difference?

So far we've exported data (just the numbers and columns in a file). But sometimes you want to share the whole notebook — the code, the charts, the explanations, and the outputs together.

How to export

In Jupyter: click File → Download as → pick your format.

PDF

For reports and presentations. Looks professional.

HTML

A webpage. Anyone can open it in a browser.

.py

Just the code, no outputs. For developers.

ipynb

The raw notebook file — for other Jupyter users.

Real-life example

You've analyzed your class's test scores in a notebook. Your teacher wants to see the work. You export as HTML — she opens it in Chrome and sees everything: the code, the table, the chart. No Python needed on her end.

HTML export is the easiest way to share a notebook with someone who doesn't use Python. They just open the file in any browser — no setup required.

Quick quiz

Let's see what stuck

Customer Reviews

Drop a review

Post a Comment