Reading & Exploring Data
Overview
Learn how to read CSV and JSON files, inspect tabular properties, and check summaries statistics.
Learning Objectives
- Load CSV and JSON file contents into DataFrames.
- Inspect DataFrame shape, columns names, and column types.
- Get statistics summaries using describe() and info().
Concept Explanation
Pandas provides readers like read_csv() and read_json(). Once loaded, you can check table shape properties (rows, cols), print the first few rows using head(), list data types using dtypes, and generate quick statistics using describe() and info().
Code Examples
Example 1 — Basics
This example introduces the fundamental syntax and concepts.
import pandas as pd
# df = pd.read_csv('data.csv')
# print(df.head())
Example 2 — Everyday Usage
This example demonstrates a realistic scenario handling business parameters.
# Load CSV string data and inspect column data types
import io
csv_data = """id,name,rating
1,Book,4.5
2,Pen,nan
"""
df = pd.read_csv(io.StringIO(csv_data))
print('Shape:', df.shape)
print('Data types:\n', df.dtypes)
print('Summary Stats:\n', df.describe())
Example 3 — Practical Data Engineering Example
This example shows clean, production-grade code structure following senior development standards.
# Clean load specific columns and display info summary
raw_data = """timestamp,user_id,action
2026-01-10,101,login
2026-01-11,102,logout
"""
df = pd.read_csv(io.StringIO(raw_data), usecols=['user_id', 'action'])
print('Columns list:', df.columns.tolist())
df.info()
Visual Flow
The following execution flow represents the step-by-step evaluation inside the interpreter:
Read file (read_csv) → Allocate DataFrame columns → Run head() (Row sample) → Print info() schema summary
Common Mistakes
Review these common pitfalls when working with this topic:
- Trying to run shape as a function (e.g. df.shape(); it is a property: df.shape).
- Loading massive CSV files into memory without specifying columns limits.
- Assuming describe() includes string columns (it ignores them by default).
- Forgetting to inspect null counts using info().
Best Practices
Enforce these Pythonic best practices in your codebase:
df.to_csv('out.csv') # Exposes index column in exported CSV
df.to_csv('out.csv', index=False) # Excludes index column in CSV output
Performance Notes
Keep these optimization guidelines in mind for performance-sensitive hotpaths:
- For large datasets, use the chunksize parameter in read_csv() to load and process data in manageable batches.
Quick Revision
Use these key summaries for last-minute revision:
- read_csv() and read_json() load datasets.
- head() and tail() view row entries.
- shape property returns table dimensions (rows, cols).
- info() details null frequencies and column datatypes.
- Always use index=False when exporting CSV files.