Mathew K Analytics

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…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Importing 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))
(891, 12)
   PassengerId  Survived  Pclass  \
0            1         0       3   
1            2         1       1   
2            3         1       3   

                                                Name     Sex   Age  SibSp  \
0                            Braund, Mr. Owen Harris    male  22.0      1   
1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   
2                             Heikkinen, Miss. Laina  female  26.0      0   

   Parch            Ticket     Fare Cabin Embarked  
0      0         A/5 21171   7.2500   NaN        S  
1      0          PC 17599  71.2833   C85        C  
2      0  STON/O2. 3101282   7.9250   NaN        S  

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')
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))
(525461, 8)
  Invoice StockCode                          Description  Quantity  \
0  489434     85048  15CM CHRISTMAS GLASS BALL 20 LIGHTS        12   
1  489434    79323P                   PINK CHERRY LIGHTS        12   
2  489434    79323W                  WHITE CHERRY LIGHTS        12   

          InvoiceDate  Price  Customer ID         Country  
0 2009-12-01 07:45:00   6.95      13085.0  United Kingdom  
1 2009-12-01 07:45:00   6.75      13085.0  United Kingdom  
2 2009-12-01 07:45:00   6.75      13085.0  United Kingdom  
# Exporting DataFrame to Excel
df.head(10).to_excel('titanic_preview.xlsx', index=False)
print('Exported first 10 Titanic rows to Excel.')
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.')
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)
   name  score
0  Anna     92
1   Ben     88

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)
Empty DataFrame
Columns: [Name, Age, Sex]
Index: []
# Exporting DataFrame to SQL database
df_sample.to_sql('scores', conn, index=False, if_exists='replace')
print('Scores table saved to the database.')
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)
    
Error reading file: HTTP Error 404: Not Found

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)
    
Error: [Errno 2] No such file or directory: 'sample_data.csv'

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.