Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Class

DataFrame

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.

How it works

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.

Syntax

class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)

Parameters

NameTypeDescription
dataarray-like, Iterable, dict, or DataFrameInitial table data.
indexIndex or array-likeRow labels.
columnsIndex or array-likeColumn labels.
dtypedtypeData type to use when constructing the table.
copybool or NoneWhether to copy input data.

Returns

A pandas DataFrame object.

Key points

  • Use DataFrame for a table that can grow or change and whose columns may have different value types.
  • Rows and columns carry labels, enabling selections and operations that refer to meaningful axis names.
  • Many pandas workflows use DataFrame as the central object for tabular data manipulation.
  • Construct a table with optional source data, axis labels, a dtype, and copying behavior.
  • Use loc for label-based or Boolean filtering across rows and columns.
  • Use iloc when the desired rows or columns are specified by zero-based positions.

Common mistakes

  • Arithmetic combines values according to matching row and column labels, so differently ordered or named axes can affect results.
  • Treating columns like dictionary entries is useful, but it does not make the object an ordinary dictionary; DataFrame indexing and axis behavior still apply.

Example: Create and select a DataFrame

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.

When to use it

Build tabular datasets

Represent records with named fields and stable row labels.

Filter and project data

Keep only rows satisfying a condition and choose selected fields.

Label-aware transformations

Combine or calculate tables while preserving meaningful row and column labels.

Interview questions

QWhat is a pandas DataFrame?

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.

QWhat does the DataFrame constructor accept?

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.

QHow do loc and iloc differ?

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.Series
  • DataFrame.index
  • DataFrame.columns
  • DataFrame.loc
  • DataFrame.iloc
  • DataFrame.at
  • DataFrame.iat
  • DataFrame.to_numpy

Source: pandas.DataFrame — pandas 3.0.6 documentation. Checked 2026-10-15.