The DataFrame: Rows and Columns

Published by Brayan K

A DataFrame is a two-dimensional, table-like structure of labelled rows and columns — Pandas' core object, where every column is itself a Series and all columns share one common row index.

Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A DataFrame has two labelled axes

A table is more than a nested list. The row index and the column index identify two independent axes, and each column has its own dtype. The first shape value counts rows; the second counts columns. Use a small branch stock table so each axis has an observable meaning.

import pandas as pd

stock = pd.DataFrame({"north": [4, 2, 3], "south": [1, 5, 2]},
                     index=["pen", "book", "folder"], dtype="int64")
print(stock.index.tolist())
print(stock.columns.tolist())
print(stock.shape, stock.ndim, stock.size)
print(stock.to_dict())

# ✅ Expected output:
# ['pen', 'book', 'folder']
# ['north', 'south']
# (3, 2) 2 6
# {'north': {'pen': 4, 'book': 2, 'folder': 3}, 'south': {'pen': 1, 'book': 5, 'folder': 2}}

There are three item rows, two branch columns and six cells. size counts cells, not non-missing observations. len(stock) counts the rows. The Series: Pandas' 1-D Building Block owns the one-dimensional labelled object used for a selected column.

import pandas as pd

stock = pd.DataFrame({"north": [4, 2], "south": [1, 5]}, dtype="int64")
print(list(stock))
print(len(stock))

# ✅ Expected output:
# ['north', 'south']
# 2

A loop directly over the frame does not visit its rows. Do not infer iteration behavior from how a table is printed. Performance: vectorization & memory owns choosing efficient operations; most column calculations do not need a Python loop over every cell.

One selected column and a one-column table have different dimensions

import pandas as pd

stock = pd.DataFrame({"north": [4, 2], "south": [1, 5]},
                     index=["pen", "book"], dtype="int64")
column = stock["north"]
table = stock[["north"]]
print(type(column).__name__, column.shape)
print(type(table).__name__, table.shape)
print(column.index.tolist(), table.index.tolist())

# ✅ Expected output:
# Series (2,)
# DataFrame (2, 1)
# ['pen', 'book'] ['pen', 'book']

A single column label returns a Series when column names are unique; a list of column labels keeps a DataFrame. That distinction changes the dimensions expected by later code. Selecting Columns and Rows owns the full selection toolbox, and loc vs iloc: Label vs Position owns the two-axis indexers.

import pandas as pd

stock = pd.DataFrame({"north": [4, 2]}, dtype="int64")
print(stock["south"])

# ✅ Expected error:
# KeyError: 'south'

Different columns can have different dtypes

import pandas as pd

stock = pd.DataFrame({
    "item": pd.Series(["pen", "book"], dtype="object"),
    "quantity": pd.Series([4, 2], dtype="int64"),
    "checked": pd.Series([True, False], dtype="bool"),
})
print(stock.dtypes.astype(str).to_dict())
print(type(stock["quantity"]).__name__)

# ✅ Expected output:
# {'item': 'object', 'quantity': 'int64', 'checked': 'bool'}
# Series

A DataFrame has dtypes for its columns, not one dtype that describes every cell. An object-backed column can also contain mixed Python values, so its dtype alone does not prove a domain rule such as every value is a product name. Data Types & astype owns representation conversion and Creating DataFrames owns construction from different input shapes.

The axis argument tells an operation which dimension to reduce

import pandas as pd

stock = pd.DataFrame({"north": [4, 2, 3], "south": [1, 5, 2]},
                     index=["pen", "book", "folder"], dtype="int64")
per_branch = stock.sum(axis=0)
per_item = stock.sum(axis=1)
print(per_branch.to_dict())
print(per_item.to_dict())
print(int(per_branch.sum()), int(per_item.sum()))

# ✅ Expected output:
# {'north': 9, 'south': 8}
# {'pen': 5, 'book': 7, 'folder': 5}
# 17 17

axis=0 reduces down the rows, leaving one result per column. axis=1 reduces across the columns, leaving one result per row. The remaining labels reveal what each answer describes. These examples select only numeric quantities so an unrelated text column cannot change the operation’s meaning.

The two grand totals agree because both sum the same six bounded integer cells. That agreement is a useful check here, not proof that a source dataset is complete or unduplicated. Aggregation & Transformation owns combining and grouping aggregation operations.

Missing cells, empty axes and duplicate labels are different conditions

import pandas as pd

unknown = pd.DataFrame({"quantity": pd.Series([None, None], dtype="Int64")})
no_rows = pd.DataFrame({"quantity": pd.Series([], dtype="Int64")})
print(unknown.shape, unknown.empty, unknown.size)
print(no_rows.shape, no_rows.empty, no_rows.size)
print(unknown.count().to_dict())

# ✅ Expected output:
# (2, 1) False 2
# (0, 1) True 0
# {'quantity': 0}

empty is true when an axis has length zero. It is not a check for all values being missing, and size still counts cells that hold missing values. Handling Missing Data (NaN, fillna, dropna) owns deciding what to do with those observations.

import pandas as pd

frame = pd.DataFrame([[4, 1], [2, 5]], columns=["quantity", "quantity"])
print(frame.columns.is_unique)
print(type(frame["quantity"]).__name__, frame["quantity"].shape)

# ✅ Expected output:
# False
# DataFrame (2, 2)

Pandas permits repeated row and column labels. If a function expects one unique column, validate that boundary explicitly. A numbered RangeIndex also does not certify that business records are unique: two rows can have different row numbers and still describe the same duplicated order. Dropping & Deduplicating Rows owns record deduplication.

Guided exercise: choose the axis from the question

Compute one total per branch and one total per item. Complete the literal hints and inspect which labels remain.

import pandas as pd

stock = pd.DataFrame({"north": [4, 2], "south": [1, 5]},
                     index=["pen", "book"], dtype="int64")
# 👉 replace ___ with 0
print(stock.sum(axis=___).to_dict())
# 👉 replace ___ with 1
print(stock.sum(axis=___).to_dict())
# 👉 replace ___ with shape
print(stock.___)

# ✅ Expected output:
# {'north': 6, 'south': 6}
# {'pen': 5, 'book': 7}
# (2, 2)

Build brief: return branch and item totals with an explicit table contract

Implement branch_totals(frame). Accept a DataFrame with unique, nonempty string row labels and exactly the unique columns north then south. Both columns must have int64 dtype. Accept at most 100 rows, with every quantity from 0 through 10000. Return two Series: item_total in row order and branch_total in column order. Do not change the input. A correctly typed empty table is valid.

import pandas as pd

def branch_totals(frame):
    if not isinstance(frame, pd.DataFrame):
        raise TypeError("DataFrame required")
    if not frame.index.is_unique or not frame.columns.is_unique:
        raise ValueError("row and column labels must be unique")
    if frame.columns.tolist() != ["north", "south"]:
        raise ValueError("columns must be north then south")
    if any(not isinstance(label, str) or not label for label in frame.index):
        raise ValueError("nonempty string row labels required")
    if any(str(dtype) != "int64" for dtype in frame.dtypes):
        raise ValueError("int64 branch quantities required")
    if len(frame) > 100 or bool(((frame < 0) | (frame > 10000)).any().any()):
        raise ValueError("at most 100 rows with quantities from 0 through 10000")
    per_item = frame.sum(axis=1)
    per_branch = frame.sum(axis=0)
    per_item.name = "item_total"
    per_branch.name = "branch_total"
    return per_item, per_branch

stock = pd.DataFrame({"north": [4, 2, 3], "south": [1, 5, 2]},
                     index=["pen", "book", "folder"], dtype="int64")
before = stock.copy()
items, branches = branch_totals(stock)
print(items.to_dict(), items.name)
print(branches.to_dict(), branches.name)
print(stock.equals(before))
try:
    branch_totals(stock[["south", "north"]])
except ValueError as error:
    print(type(error).__name__ + ": " + str(error))

# ✅ Expected output:
# {'pen': 5, 'book': 7, 'folder': 5} item_total
# {'north': 9, 'south': 8} branch_total
# True
# ValueError: columns must be north then south

Test one row, the typed empty table, reversed column order, duplicate labels, a missing value, a non-integer dtype, a negative quantity and the maximum bounds. The limits keep the exercise’s sums safely within int64; they are an exercise contract, not a universal Pandas limit.

Practice quiz

What does this program print? import pandas as pd f = pd.DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]}) print(f.shape, f.size)

  • (2, 3) 6
  • (3, 2) 6
  • (2, 3) 2
  • (2, 3) 3

Answer: (2, 3) 6. Shape counts each axis; size counts all cells.

What does this program print? import pandas as pd f = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) print(list(f))

  • [0, 1]
  • ['a', 'b']
  • [[1, 3], [2, 4]]
  • [1, 2, 3, 4]

Answer: ['a', 'b']. Direct frame iteration yields column labels.

What does this program print? import pandas as pd f = pd.DataFrame({"a": [1, 2]}) print(type(f[["a"]]).__name__, f[["a"]].shape)

  • Series (2,)
  • DataFrame (1, 2)
  • DataFrame (2, 1)
  • Series (2, 1)

Answer: DataFrame (2, 1). A list of column names retains the two-dimensional table.

What does this program print? import pandas as pd f = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=["x", "y"]) print(f.sum(axis=1).to_dict())

  • {'a': 3, 'b': 7}
  • {'x': 3, 'y': 7}
  • {'a': 4, 'b': 6}
  • {'x': 4, 'y': 6}

Answer: {'x': 4, 'y': 6}. Reducing across columns leaves one total for each row.

What does this program print? import pandas as pd f = pd.DataFrame({"a": pd.Series([None], dtype="Int64")}) print(f.empty, f.size)

  • False 1
  • True 0
  • True 1
  • False 0

Answer: False 1. A missing cell still occupies an existing row and column.

What does this program print? import pandas as pd f = pd.DataFrame([[1, 2]], columns=["a", "a"]) print(f.columns.is_unique)

  • True
  • False
  • None
  • ValueError

Answer: False. Duplicate column labels are allowed unless a boundary rejects them.

Why check row and column labels before producing a table report?

  • Pandas cannot store string labels
  • Every printed table is already validated
  • Labels determine what each result describes and may be duplicated
  • The index always proves business-key uniqueness

Answer: Labels determine what each result describes and may be duplicated. A structurally valid table can violate the caller’s identity assumptions.

Which operation belongs to the detailed selection lesson?

  • Recognizing that shape has two dimensions
  • Recognizing a column Series
  • Knowing that columns may have different dtypes
  • The full set of row/column selection and slicing rules

Answer: The full set of row/column selection and slicing rules. This lesson owns the table model and links to the owner of detailed selection.

Continue this course

Related lessons