DataFrame#
pandas.DataFrame is a 2-D labelled tabular container: rows
indexed by a row-index, columns indexed by a column-index, each
column a Series of one dtype. Third-party
(pip install pandas), not in the standard library. The
operator’s default container for any analysis that fits in
memory; for larger-than-memory workloads, polars or
duckdb cover the same structure.
Construct from a dict of column-name → list.
import pandas as pd
df = pd.DataFrame({
"host": ["a", "b", "c"],
"port": [80, 443, 22],
"open": [True, True, False],
})
Construct from a list of records.
df = pd.DataFrame([
{"host": "a", "port": 80, "open": True},
{"host": "b", "port": 443, "open": True},
{"host": "c", "port": 22, "open": False},
])
Read from CSV.
df = pd.read_csv("hosts.csv")
Read from JSON Lines.
df = pd.read_json("events.ndjson", lines=True)
Select a column as a Series.
df["port"]
Select multiple columns as a sub-DataFrame.
df[["host", "port"]]
Label-based row selection with .loc.
df.loc[df["open"], ["host", "port"]]
Positional row selection with .iloc.
df.iloc[0:5]
Filter with a boolean mask.
df[df["port"] >= 443]
Add a computed column.
df["is_https"] = df["port"] == 443
Group and aggregate.
df.groupby("open")["port"].mean()
Merge two DataFrames on a key.
merged = pd.merge(df, scans, on="host", how="left")
Pivot long → wide.
wide = events.pivot_table(index="host", columns="event", values="count", aggfunc="sum")
Method catalog#
The everyday surface, grouped by purpose.
Shape and inspection#
Method |
Effect |
|---|---|
|
|
|
The column index. |
|
Per-column dtype. |
|
Memory + dtype summary. |
|
Summary statistics on numeric columns. |
|
First |
|
Last |
|
Random sample. |
Filter and transform#
Method |
Effect |
|---|---|
|
Filter rows by a string expression. |
|
Return a copy with new / replaced columns. |
|
Rename columns or index. |
|
Drop columns (or rows with |
|
Deduplicate rows. |
|
Sort by one or more columns. |
|
Apply |
|
Element-wise transform. |
Group, join, reshape#
Method |
Effect |
|---|---|
|
Split-apply-combine; |
|
SQL-style join. |
|
Stack along an axis. |
|
Long → wide aggregation. |
|
Wide → long. |
|
Move a level between index and columns. |
Missing data#
Method |
Effect |
|---|---|
|
Boolean masks. |
|
Drop rows / columns with missing values. |
|
Replace missing values. |
|
Fill via interpolation along an axis. |
I/O#
Method |
Effect |
|---|---|
|
CSV round-trip. |
|
JSON round-trip. |
|
Columnar binary; preferred for large data. |
|
Round-trip against any DBAPI connection. |
|
Underlying numpy 2-D array. |
|
Convert to a dict (records, list, index, etc.). |
References#
Series for the 1-D building block.
Records for the lightweight alternatives (
namedtuple,@dataclass,TypedDict) when one row is the unit.Frameworks for the data ecosystem
DataFramesits in (numpy,pandas,polars,jupyter).