HOWTO · Pandas

How to Add Column With a Constant Value in Pandas

Learn how to add constant-valued columns to a pandas DataFrame with bracket assignment, loc, assign(), and index-aligned Series.

On this page

To add a column containing the same value in every row, assign a scalar to a new column label: df["status"] = "active". Pandas broadcasts that scalar across the DataFrame’s index. This guide compares bracket assignment, loc, and assign(), then explains when a Series is useful and why apply() is usually unnecessary. Bracket assignment is the simplest choice when you want to update the DataFrame you already have; use assign() when you want a returned DataFrame that can be chained.

Add a constant column with bracket assignment

Bracket assignment is the direct default. The example builds a small DataFrame with a stable index, assigns one scalar, and prints the result. This assignment changes df; it is not a special in-place mode of the syntax. If a column with that label already exists, assignment replaces its values.

Run this example:

import pandas as pd

df = pd.DataFrame({"item": ["pen", "book"]}, index=[1, 2])
df["price"] = 5
print(df.to_string())

The scalar 5 is repeated for each index label. In your own code, replace price and 5 with the column name and value you need. The DataFrame retains its existing rows and index.

Add a column with .loc

loc selects rows and columns by label. Use df.loc[:, "price"] = 5 to select every row (:) and the price column, then assign the scalar. It gives an explicit row-and-column selection, but for adding a single column it is generally equivalent in purpose to df["price"] = 5 and is not required.

Add one or more columns with DataFrame.assign()

DataFrame.assign() returns a DataFrame containing the added or replaced columns. Capture that return value, or use it in a chain; calling assign() without using its result does not update the original variable.

For example, df = df.assign(price=5) binds the returned DataFrame back to df. To build several columns with the same value, pass a dictionary made by dict.fromkeys(): df = df.assign(**dict.fromkeys(["in_stock", "featured"], True)). dict.fromkeys() is a dictionary method, and ** expands its keys as keyword arguments. For different values, pass a dictionary such as df = df.assign(**{"tax": 1, "shipping": 3}).

Use Series when index alignment matters

A Series is useful when each value belongs to a particular index label. Pandas aligns its values to the DataFrame by index label during column assignment, rather than pairing values solely by their position. Labels in the DataFrame that have no matching value in the Series receive a missing value; labels that occur only in the Series do not create extra DataFrame rows.

import pandas as pd

df = pd.DataFrame({"item": ["pen", "book"]}, index=[1, 2])
prices = pd.Series([5, 8], index=[2, 3])
df["price"] = prices
print(df.to_string())

Here the value for label 2 aligns with the book row. Label 1 has no matching value, so its new column entry is missing. If every row should receive the same scalar, direct assignment is clearer and avoids alignment surprises.

Why DataFrame.apply() is usually unnecessary

DataFrame.apply() can run a function across rows or columns, but calling a function once per row just to return the same constant adds work and obscures the intent. For this task, prefer df["price"] = 5, which expresses scalar broadcasting directly. Reserve apply() for calculations whose result depends on row or column data.

Choose an approach for your task

Use bracket assignment when you want the shortest direct update to an existing DataFrame. Use loc when the row-and-column selection is part of a larger label-based operation. Use assign() when its returned DataFrame fits a chain or when constructing several columns from keyword arguments. Use a Series when values should match rows by index label. For a constant shared by all rows, scalar assignment is the clearest option; apply() is unnecessary.