Data Loading & Saving
Stop manually entering data. Learn to read and write CSV, Excel, and JSON files with Pandas.
Stop manually entering data. Learn to read and write CSV, Excel, and JSON files with Pandas. This hands-on tutorial focuses on practical implementation of data loading & saving concepts.
Module 2: Data Loading & Saving
Real-world data comes from files: CSVs, Excel spreadsheets, SQL databases, and JSON APIs. Pandas allows you to load this data with a single line of code.
Lesson 3: Reading Data
Pandas supports a huge number of input formats using functions that start with read_.
Common Readers:
pd.read_csv("file.csv"): The most common format.pd.read_excel("file.xlsx"): Reads Excel sheets.pd.read_json("file.json"): Reads JSON strings or files.pd.read_html("url"): Scrapes all<table>tags from a webpage!
Key Parameters:
header=0: Takes the first row as column names (default).index_col=0: Uses the first column as the index.sep=",": Specifies the delimiter (useful for TSV files).
Lesson 4: Writing Data
Saving data is just as easy using methods that start with to_.
Common Writers:
df.to_csv("new_file.csv", index=False): Saves to CSV.index=Falseprevents writing the row numbers.df.to_excel("report.xlsx"): Requiresopenpyxl.df.to_json("data.json"): Useful for web apps.
Mini Project: Data Converter
Challenge: Imagine you received data in JSON format but need to send it to your boss as a CSV.
- Create a DataFrame from a list of dictionaries (JSON-like).
- "Save" it to a CSV string without the index numbers.
Quiz
Question 1 of 5Which parameter should you use in to_csv() to avoid saving the row numbers (0, 1, 2...)?
Key Takeaways
✅ read_csv is your bread and butter.
✅ Use index=False when saving CSVs to keep them clean.
✅ Pandas can handle JSON, Excel, SQL, and even HTML tables out of the box.