Lesson 7 · Mastering Pandas
How to Import and Export Data in Pandas: CSV, Excel, JSON, and SQL Explained
In this lesson, you will learn how to import and export different types of data using Pandas: CSV, Excel, JSON, and SQL formats. Working with these formats…
- CourseMastering Pandas
- Lesson7 of 44
- Video17 min
- FormatJupyter notebook · 11 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbImporting and Exporting Data in Pandas: Intermediate Beginner Lesson#
In this lesson, you will learn how to import and export different types of data using Pandas: CSV, Excel, JSON, and SQL formats. Working with these formats is essential for real-world projects in data science and analytics.
import warnings
warnings.filterwarnings('ignore')
# Data setup (Titanic Dataset)
import pandas as pd
import numpy as np
np.random.seed(42)
url = 'https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'
df = pd.read_csv(url)
print(df.shape)
print(df.head(3))
Reading a CSV File#
CSV (comma-separated values) is one of the most common file formats for tabular data. Pandas makes it simple to read a CSV file and work with its contents.
# Loading a local CSV file
# Uncomment and adjust the file path below to use your own CSV file
# df_local = pd.read_csv('path_to_your_file.csv')
# print(df_local.head(3))
Exporting Data to CSV#
After cleaning or analyzing data, it is common to export your DataFrame back to a CSV file so you can share results or use them elsewhere.
# Saving DataFrame to CSV
df.to_csv('titanic_preview.csv', index=False)
print('File saved: titanic_preview.csv')
Importing Excel Files#
Many real-world datasets come in Excel format (.xlsx). Pandas lets you easily open spreadsheets and select which sheet to import using pd.read_excel().
# Downloading and reading Excel
excel_url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00502/online_retail_II.xlsx'
df_excel = pd.read_excel(excel_url, sheet_name='Year 2009-2010')
print(df_excel.shape)
print(df_excel.head(3))
# Exporting DataFrame to Excel
df.head(10).to_excel('titanic_preview.xlsx', index=False)
print('Exported first 10 Titanic rows to Excel.')
Reading and Writing JSON Data#
JSON (JavaScript Object Notation) is used for structured data exchange between applications and web services. It looks like Python dictionaries, which makes it a great fit for pandas.
# Creating a small DataFrame and exporting to JSON
sample_dict = {'name': ['Anna', 'Ben'], 'score': [92, 88]}
df_sample = pd.DataFrame(sample_dict)
df_sample.to_json('scores.json', orient='records', lines=True)
print('scores.json created with two entries.')
# Importing data from a JSON file
df_json = pd.read_json('scores.json', orient='records', lines=True)
print(df_json)
Importing Data from SQL Databases#
SQL (Structured Query Language) databases are used for storing large amounts of data. Using pandas, you can connect to a database and load data via SQL queries.
# Creating and reading from a SQLite database
import sqlite3
conn = sqlite3.connect(':memory:')
df.head(5).to_sql('passengers', conn, index=False)
query = 'SELECT Name, Age, Sex FROM passengers WHERE Age < 20'
df_sql = pd.read_sql(query, conn)
print(df_sql)
# Exporting DataFrame to SQL database
df_sample.to_sql('scores', conn, index=False, if_exists='replace')
print('Scores table saved to the database.')
Handling File Encoding and Errors#
Sometimes, files use different character encodings or are missing data. Pandas can handle these cases by specifying extra parameters when loading files.
# Reading a CSV file with a different encoding
try:
url = 'https://raw.githubusercontent.com/plotly/datasets/master/superstore.csv'
df_store = pd.read_csv(url, encoding='latin1')
print(df_store.head(2))
except Exception as e:
print('Error reading file:', e)
Practice: Try Importing Your Own Data#
Now is a great time to experiment: import any CSV, Excel, or JSON file you have. Explore it with df.shape and df.head().
# User input: Importing a custom CSV file
file_name = input('Enter a CSV file path: ')
try:
df_user = pd.read_csv(file_name)
print(df_user.shape)
print(df_user.head(3))
except Exception as e:
print('Error:', e)
Wrapping Up: Best Practices#
- Always check the shape and a preview of data after importing.
- Watch for file encoding errors and missing data.
- Use clear, meaningful file names when saving your work.
- Take note of where your data came from and how you exported it.
Thank You & Next Steps#
Practice importing and exporting different file types from this lesson.
Subscribe to the channel for more pandas and real-world data skills!
Leave a comment sharing your biggest takeaway or any challenges you faced.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



