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

Pandas · Pipe

df.columns

DataFrame.columns gives access to a DataFrame’s column labels as a pandas Index. Read it to inspect labels and assign a new label sequence to rename the columns.

How it works

df.columns exposes a DataFrame’s column labels as a pandas Index. Read it to inspect labels or use it in selection and alignment workflows; assigning a same-length label sequence replaces the DataFrame’s column names.

Syntax

DataFrame.columns

Returns

pandas.Index — the column labels of the DataFrame.

Key points

  • df.columns identifies the labels along the DataFrame’s column axis.
  • Use the property to discover a DataFrame’s current column names before selecting or transforming data.
  • The property returns a pandas Index, preserving the column-label axis abstraction.
  • Column labels participate in selecting data, renaming fields, and aligning DataFrame operations.

Common mistakes

  • The result is an Index rather than a regular Python list, so use Index operations or explicitly convert it when an ordinary list is required.
  • Treat the returned labels as immutable; rename columns by assigning a new label sequence to df.columns instead of mutating individual Index entries.

Example: Inspect and rename DataFrame columns

import pandas as pd

df = pd.DataFrame({"name": ["Ada", "Linus"], "score": [98, 95]})

print(df.columns)
print(df["score"])

df.columns = ["student", "points"]
print(df)

Inspect the column labels, select a column by label, then replace all labels with a new same-length sequence.

When to use it

Discover schema

Before selecting fields dynamically, inspect available labels and compare them with required names.

Rename all columns

Replace the complete set of column names after loading data with inconsistent headers.

Label-aware manipulation

Use column labels when building selections or aligning DataFrame operations.

Interview questions

QWhat does df.columns return?

DataFrame.columns returns the labels for the DataFrame’s columns as a pandas Index. It represents the column names rather than the row index.

Follow-up: How would you obtain a plain Python list? Convert the result explicitly, for example with df.columns.tolist().

QHow can df.columns be used to rename columns?

Use df.columns to inspect the current labels, and assign a new sequence to rename all columns. The replacement labels should correspond one-for-one with the existing columns.

Follow-up: How would you rename only one column? Use a label-mapping operation such as df.rename(columns={old: new}), preserving the other labels.

QHow does df.columns differ from df.index?

Column labels are separate from row labels: df.columns describes the DataFrame’s column axis, while df.index describes its row axis. Both are axis-label objects used by DataFrame operations.

Follow-up: What related attribute exposes both axes? df.axes returns a list representing the DataFrame’s axes.

  • DataFrame.index
  • DataFrame.axes

Source: pandas.DataFrame.columns. Checked 2026-10-15.