Work with CSV, Excel, Parquet, and SQL

3.1.1

Choose an input format and control types, worksheets, or columns explicitly.

On this page

Pick a format for the handoff

CSV exchanges plain text but requires explicit dtype and missing-value rules. Excel is useful when people need worksheets. Parquet stores typed columns and can read only selected columns. Install a compatible optional engine such as openpyxl for .xlsx or pyarrow for Parquet.

import pandas as pd

sales = pd.DataFrame({"item": ["Tea", "Cake"], "units": [4, 2]})
print(sales.to_csv(index=False).strip())
Example output
item,units
Tea,4
Cake,2

Read query results into pandas

Use read_sql_query when a database should filter or aggregate data first. Use query parameters for values. This example uses an in-memory SQLite database and needs no database server.

from contextlib import closing
import sqlite3
import pandas as pd

with closing(sqlite3.connect(":memory:")) as db:
    db.execute("CREATE TABLE sales (item TEXT, units INTEGER)")
    db.executemany("INSERT INTO sales VALUES (?, ?)", [("Tea", 4), ("Cake", 2)])
    rows = pd.read_sql_query("SELECT units FROM sales WHERE item = ?", db, params=("Tea",))
print(rows["units"].tolist())
Example output
[4]

Official references