Build tabular datasets
Represent records with named fields and stable row labels.
Pandas · Class
A DataFrame is pandas’ mutable, two-dimensional table with labeled rows and columns. Its columns can hold different data types, and arithmetic aligns values by both axis labels.
DataFrame constructs a mutable two-dimensional table whose rows and columns have labels and whose columns may hold different data types. Operations use those labels for alignment, while indexing tools such as loc and iloc select data by labels or integer positions.
class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)| Name | Type | Description |
|---|---|---|
data | array-like, Iterable, dict, or DataFrame | Initial table data. |
index | Index or array-like | Row labels. |
columns | Index or array-like | Column labels. |
dtype | dtype | Data type to use when constructing the table. |
copy | bool or None | Whether to copy input data. |
A pandas DataFrame object.
import pandas as pd
df = pd.DataFrame(
{"name": ["Ada", "Linus"], "score": [98, 87]},
index=["a", "b"],
)
df["passed"] = df["score"] >= 90
print(df.loc[df["passed"], ["name", "score"]])Creates a labeled table from column data, derives a Boolean column, and selects rows and columns by labels.
Represent records with named fields and stable row labels.
Keep only rows satisfying a condition and choose selected fields.
Combine or calculate tables while preserving meaningful row and column labels.
DataFrame is pandas' primary two-dimensional data structure. It is size-mutable, can contain heterogeneous column types, and has labeled row and column axes.
Follow-up: How are arithmetic operations matched? They align values on both row and column labels rather than relying only on physical position.
The constructor accepts data plus optional index, columns, dtype, and copy arguments. Data can therefore be shaped and labeled explicitly when the table is created.
Follow-up: Which constructor argument controls the row labels? The index argument supplies them; columns supplies the column labels.
loc selects by label or Boolean array, whereas iloc selects by integer position. Use loc when axis labels define the selection and iloc when positions define it.
Follow-up: How would you select one cell by each style? Use at for a single row/column label pair and iat for a single integer-position pair.
pandas.SeriesDataFrame.indexDataFrame.columnsDataFrame.locDataFrame.ilocDataFrame.atDataFrame.iatDataFrame.to_numpy