Reading & Writing Data

Python · Pandas Basics

Theory

pandas reads files straight into a DataFrame. pd.read_csv() takes a file path, a URL or any file-like object, splits each line on the separator, uses the first line as column names and infers a dtype for every column. Useful options: sep for other delimiters, usecols to load only some columns, dtype to force types, parse_dates to turn text into datetimes, na_values for extra missing markers and nrows to read a sample. For practice, io.StringIO(text) lets you read CSV text held in a string.

Writing is the mirror image: df.to_csv('out.csv', index=False) saves a file; leave out index=False and the row index is written as an extra unnamed column. JSON uses read_json()/to_json(orient='records'). read_excel()/to_excel() need the openpyxl package, read_parquet() needs pyarrow, and read_sql() takes a database connection plus a query.

Why we use it

Data almost always arrives as a file or a query result. read_* functions turn it into a typed DataFrame in one line, and to_* functions hand your results back to Excel, a dashboard or another pipeline.

When to use it

Use read_csv for CSV/TSV exports, read_json for API output, read_excel for spreadsheets and read_sql for databases. For very large files read in chunks (chunksize=) or with usecols, or use a columnar format like Parquet.

How it works

  1. pd.read_csv(source)
  2. Read the header line as column names
  3. Split each line on sep into fields
  4. Infer a dtype per column; mark NA values as NaN
  5. parse_dates or dtype given? Yes: Convert those columns as asked. No: Keep inferred types.
  6. DataFrame ready; later df.to_csv(..., index=False)

Syntax

pd.read_csv(path_or_buffer, sep=',', usecols=[...], parse_dates=[...])
df.to_csv(path, index=False)
pd.read_json(buffer) / df.to_json(orient='records')

Example

df = pd.read_csv('orders.csv', parse_dates=['order_date'])
df.to_csv('clean_orders.csv', index=False)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic