Lesson 6 · Python For Time Series
How to Import and Export CSV, Excel, and JSON Files in Python for Data Management
In this lesson, you will learn how to bring real-world data into Python using CSV, Excel, and JSON files, and how to save your work back into these formats.…
- CoursePython For Time Series
- Lesson6 of 30
- Video11 min
- FormatJupyter notebook · 22 code cells
- Data2 datasets
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- mydata.csv28 B
- shampoo_sales_example.xlsx5.9 KB
📓 Full notebook
Download .ipynb
Welcome to Python: Importing and Exporting CSV, Excel, and JSON Files#
In this lesson, you will learn how to bring real-world data into Python using CSV, Excel, and JSON files, and how to save your work back into these formats.
We will work through practical examples using common data types, including a real retail sales dataset.
No experience is needed. Let us start exploring Python data handling together!
# Let us start by turning off extra warning messages for clarity.
import warnings
warnings.filterwarnings("ignore")
What are CSV, Excel, and JSON files?#
CSV stands for Comma-Separated Values. Each line is a row, and values are separated by commas.
Excel files have a .xlsx or .xls ending and store data in spreadsheets.
JSON stands for JavaScript Object Notation. It stores data as readable text using key-value pairs.
You will see how to work with each of them step-by-step.
# To work with data files, let us import the pandas library.
import pandas as pd
# Data setup
# Let us download a simple retail sales CSV from the web.
csv_url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv"
df = pd.read_csv(csv_url)
# Show the first few rows to preview our data
print(df.head())
# Show the shape of the table
print("Shape:", df.shape)
# Let us plot the sales data to spot trends over time.
import matplotlib.pyplot as plt
df["Sales"].plot(title="Shampoo Sales Over Time")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.show()
Reading CSV Files#
CSV files are one of the most popular ways to share data.
You have already loaded a CSV file from the web.
Next, you will learn how to read a CSV file stored on your own computer.
# Read a local CSV file (make sure your file path is correct).
local_path = "mydata.csv"
try:
df_local = pd.read_csv(local_path)
print(df_local.head())
except FileNotFoundError:
print("File not found. Please check the file path!")
# Writing DataFrame back to a new CSV file
df.to_csv("shampoo_sales_copy.csv", index=False)
print("Data saved to shampoo_sales_copy.csv")
# Reading and writing Excel files
excel_path = "shampoo_sales_example.xlsx"
df.to_excel(excel_path, index=False)
print(f"Saved as {excel_path}")
df_from_excel = pd.read_excel(excel_path)
print(df_from_excel.head())
JSON Files in Python#
JSON is used a lot on the web for sharing data between websites and apps.
Pandas can read and write JSON just like CSV or Excel.
Let us look at some simple examples next.
# Writing to JSON
df.to_json("shampoo_sales.json", orient="records", lines=True)
print("Saved to shampoo_sales.json")
# Reading JSON back in
df_json = pd.read_json("shampoo_sales.json", orient="records", lines=True)
print(df_json.head())
# Handling CSV files with missing or extra columns
# To demonstrate handling, we create a new sample CSV directly.
import io
csv_sample = "Month,Sales\n1-01,266.0\n2-01,145.9"
# This CSV has only 2 columns, but we request 3 column names.
try:
df_missing = pd.read_csv(io.StringIO(csv_sample), names=["Month", "Sales", "Store"], header=0)
print(df_missing.head())
except pd.errors.ParserError as e:
print("Error reading CSV:", e)
# Try replacing missing values if any
df_filled = df.fillna(0)
print(df_filled.head())
# Filtering rows that meet a condition
over_350 = df[df["Sales"] > 350]
print(over_350)
# Select only a few columns for a new table
small_table = df[["Month", "Sales"]].copy()
print(small_table.head())
# Exporting part of the data to Excel
small_table.to_excel("mini_sales.xlsx", index=False)
print("Exported smaller table to mini_sales.xlsx")
# Merging two DataFrames
extra_df = pd.DataFrame({
"Month": ["3-01", "7-01"],
"Return Rate": [2.1, 1.7]
})
merged = pd.merge(df, extra_df, on="Month", how="left")
print(merged.head(8))
# Challenge: Let us ask the user to read their own CSV file
filename = input("Type the CSV file name to read: ")
try:
user_df = pd.read_csv(filename)
print("Here is your data:")
print(user_df.head())
except FileNotFoundError:
print("File not found. Please check the file name and try again!")
# Mini-Project Part 1: Monthly Averages
df["Sales"] = pd.to_numeric(df["Sales"], errors="coerce")
average = df['Sales'].mean()
print(f"Average shampoo sales: {average:.2f}")
# Mini-Project Part 2: Export the result to JSON
import json
output = {'average_sales': average}
with open("average_sales.json", "w") as f:
json.dump(output, f)
print("Saved the average sales to average_sales.json")
# Troubleshooting tip: What if your file is not found?
try:
df_try = pd.read_csv("wrongname.csv")
except FileNotFoundError:
print("Oops. The file name is wrong or not in this folder.")
# Useful pandas options for exporting to Excel or CSV
df.to_csv("shampoo_sales_utf8.csv", encoding="utf-8", index=False)
df.to_excel("shampoo_sales_no_index.xlsx", index=False, sheet_name="SalesData")
print("Saved with special options.")
Recap: What You Learned#
- How to read and write CSV, Excel, and JSON files in Python
- How to preview your data and handle missing values
- How to combine and filter tables for your project
- Tips for troubleshooting file errors
You are ready to use Python for loading and saving real data!
Thank You for Learning!#
Ready to practice? Try importing data for your favorite topic and export it in a new format!
If you liked this lesson, subscribe to the channel and comment below. Let us know what you want to learn next!
Happy coding!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



