Pandas — Reading CSV & Excel Files
Master the standard workflow every Data Analyst follows to ingest real-world data: read plain-text CSV files, load multi-sheet Excel workbooks, configure delimiters, and perform immediate structural inspection.
These files are mounted in your browser environment. Your Python code will read directly from them!
1. Why Read CSV & Excel with Pandas?
In real business environments, data does not arrive neatly typed inside Python scripts. Over 80% of corporate records exist as CSV export files or Excel workbooks.
2. Reading a CSV File: pd.read_csv()
The pd.read_csv() function reads comma-separated values from disk and automatically constructs a DataFrame:
# Reads 'sales.csv' and returns a 2D DataFrame
df = pd.read_csv("sales.csv")
print(df)
3. 🔥 Live CSV Ingestion Lab
Read the mounted file "sales.csv" into variable df and print the resulting DataFrame:
4. Important CSV Parameters
Real files often diverge from standard defaults. These 5 parameters solve 95% of real-world ingestion quirks:
sep=";" for European files or sep="\t" for tabs.header=None if file lacks headers.["Product", "Price"]).df = pd.read_csv("inventory_semicolon.csv", sep=";", usecols=["Item", "Stock"])
5. Reading Excel Spreadsheets: pd.read_excel()
Spreadsheets (.xlsx) are read using pd.read_excel(). By default, it imports the first worksheet:
print(df.head())
6. Selecting Excel Worksheets: sheet_name
Excel workbooks frequently contain multiple quarterly tabs. The sheet_name parameter allows targeted ingestion:
q1_df = pd.read_excel("retail_store.xlsx", sheet_name="Q1_Sales")
# 2. Read specific sheet by 0-based integer index
q2_df = pd.read_excel("retail_store.xlsx", sheet_name=1)
# 3. Read ALL sheets (returns a Python dictionary of DataFrames)
all_sheets = pd.read_excel("retail_store.xlsx", sheet_name=None)
7. CSV vs. Excel Comparison
Both formats are ubiquitous in business analytics. Here is how they compare in practice:
- Plain text format, human-readable in any text editor
- Single table per file
- Extremely fast load times & lightweight storage
- Universal standard for database exports and APIs
- Read with:
pd.read_csv()
- Binary XML zipped workbook format
- Can hold multiple worksheets and tabs
- Supports formulas, charts, and cell formatting
- Corporate standard for business department reporting
- Read with:
pd.read_excel()
8. After Reading: Immediate Dataset Inspection
Reading the file into memory is only step 1. Never begin writing analysis formulas until you verify that the file was parsed correctly:
print(df.head()) # Preview top 5 rows
print(df.shape) # Confirm (rows, columns) count
print(df.columns) # Verify expected header names
print(df.dtypes) # Ensure numbers weren't read as text
9. 🔥 Live Data-Loading & Inspection Workflow
Load "sales.csv", preview the top rows, check shape, column names, and data types:
10. Handling Common File Errors
When file reading fails, Python raises specific informative errors:
Caused by typing the wrong filename or path: No such file or directory: 'sale.csv'. Verify current working directory and spelling.
If a semicolon-separated file is read without sep=";", all data collapses into 1 gigantic string column.
ValueError: Worksheet named 'Sales2025' not found. Check exact casing and sheet names in Excel.
11. Practical Data-Analytics Scenario
Corporate analysts regularly receive identical customer transaction tables from two different internal teams: the engineering team exports customer_orders.csv, while the accounting team sends customer_orders.xlsx.
Whether loaded via read_csv() or read_excel(), the resulting Pandas DataFrame has the exact same structure, methods, and analytics capabilities!
12. 🔥 Final Live Challenge: Multi-Format Ingestion
Complete the two-part ingestion pipeline:
- Load
"customer_orders.csv"and inspect head and shape - Load
"customer_orders.xlsx"from worksheet"Orders"intodf_exceland print it
13. Knowledge Assessment Quiz
Test your understanding of file ingestion, delimiters, worksheets, and initial inspection:
Pandas Reading CSV & Excel Certification Quiz
Test your understanding with real-world query prediction and syntax questions.
Which pandas function is specifically designed to read standard comma-separated text files into a DataFrame?