How Do I Fill Column With One Value In Pandas?

  • Summary: The following approaches will help you to fill a column with one value in Pandas:
    • df['col_name'] = "value"
    • df.insert(index, 'col_name', 'value')
    • df.loc[:, 'col_name'] = 'value'
    • df = df.assign('col_name'='value')

Introduction

Problem Formulation: How do I fill a column with one value in Pandas?

Example: Let’s consider that we have a DataFrame as shown below:

# Importing the pandas' module as pd
import pandas as pd
# Creating a data frame
df = pd.DataFrame({
    'cola': [100, 200, 300, 400, 500], 
    'colb': ['A', 'B', 'C', 'D', 'E'],
    'colc': [True, False, True, True, False],
})
print(df)

Output:

   cola   colb   colc
0   100    A     True
1   200    B     False
2   300    C     True
3   400    D     True
4   500    E     False

Expected Output: Now that we have successfully created a Panda Dataframe, we can see that the column “cola” has different values [100, 200, 300, 400, 500]. Now suppose we want to change all these column values to only a single value, say “10“, as shown below.

     cola  colb   colc
0    10    A      True
1    10    B      False
2    10    C      True
3    10    D      True
4    10    E      False

So how do we achieve the above feat?πŸ€” Don’t worry! We got you covered as this article will answer your query. So, let’s dive into the different ways to fill/change an entire column with a specific value in Pandas.

Recommended Read: 10 Minutes to Pandas (in 5 Minutes)

Method 1: Using Square Bracket Notation []

The idea here is to select the particular column (cola in this case) and assign the value to it. To select the column, you can use the square bracket notation and specify the column name within it, for example: df['cola'] = "10".

Solution:

import pandas as pd
df = pd.DataFrame({
    'cola': [100, 200, 300, 400, 500],
    'colb': ['A', 'B', 'C', 'D', 'E'],
    'colc': [True, False, True, True, False],
})
print("Initial DataFrame: ")
print(df)
# Changing the existing value of the column "cola"
df['cola'] = "10"
print("\nData frame with a single value in cola:")
print(df)

Output:

Initial DataFrame: 
   cola colb   colc
0   100    A   True
1   200    B  False
2   300    C   True
3   400    D   True
4   500    E  False

Data frame with a single value in cola:
  cola colb   colc
0   10    A   True
1   10    B  False
2   10    C   True
3   10    D   True
4   10    E  False

Method 2: Using insert

Another approach is to use the insert method on the Pandas DataFrame to fill the column with a specific value.Β 

Syntax: df.insert(index, column name, default value)

The index parameter is used to specify the position where the new column will be inserted. Indexing starts from 0 in Python, i.e. the first column will have index 0.

Solution:

import pandas as pd
df = pd.DataFrame({
    'cola': [100, 200, 300, 400, 500],
    'colb': ['A', 'B', 'C', 'D', 'E'],
    'colc': [True, False, True, True, False],
})
print("Initial DataFrame: ")
print(df)
# Adding column "cold" at index 1 after column "cola"
df.insert(1, 'cold', '10')
print("Data frame having a column with single values:")
print(df)

Output:

Initial DataFrame: 
   cola colb   colc
0   100    A   True
1   200    B  False
2   300    C   True
3   400    D   True
4   500    E  False

Data frame having a column with single values:
   cola cold colb   colc
0   100   10    A   True
1   200   10    B  False
2   300   10    C   True
3   400   10    D   True
4   500   10    E  False

Method 3: Using loc

TheΒ locΒ property is used to get or set specified value(s) to specified labels in a Pandas DataFrame. You can access a group of rows or columns using the loc property. You can read more about the loc property here.

We will use the loc property to select all row values that belong to the column cola in our DataFrame and then assign the value to the column as shown below.

Solution

import pandas as pd

df = pd.DataFrame({
    'cola': [100, 200, 300, 400, 500],
    'colb': ['A', 'B', 'C', 'D', 'E'],
    'colc': [True, False, True, True, False],
})
print("Initial DataFrame: ")
print(df)
# Using loc to change the values of cola
df.loc[:, 'cola'] = 10
print("DataFrame after modification:")
print(df)

Output:

Initial DataFrame: 
   cola colb   colc
0   100    A   True
1   200    B  False
2   300    C   True
3   400    D   True
4   500    E  False

DataFrame after modification:
   cola colb   colc
0    10    A   True
1    10    B  False
2    10    C   True
3    10    D   True
4    10    E  False

NOTE: df.loc[:, 'cola'] will select all row values that correspond to the column value cola.

Related Read: Pandas loc() and iloc() – A Simple Guide with Video

Method 4: Using assign()

We can use the assign() method to fill the columns with a single value. Generally, the assign() method is used to add a new column to an existing DataFrame. However, you can also use it in the following way to change the values of a column to a single/specific value:

import pandas as pd

df = pd.DataFrame({
    'cola': [100, 200, 300, 400, 500],
    'colb': ['A', 'B', 'C', 'D', 'E'],
    'colc': [True, False, True, True, False],
})
print("Initial DataFrame: ")
print(df)
# Using assign() to change the values of cola
df = df.assign(cola=10)
print("DataFrame after modification:")
print(df)

Output:

Initial DataFrame: 
   cola colb   colc
0   100    A   True
1   200    B  False
2   300    C   True
3   400    D   True
4   500    E  False
DataFrame after modification:
   cola colb   colc
0    10    A   True
1    10    B  False
2    10    C   True
3    10    D   True
4    10    E  False

 ➀Adding A New Column With A Specific Value In A Pandas DataFrame

The simplest approach to add a new colmn with a specific value in an existing DataFrame is quite similar to Method 1 in this tutorial with the only difference being that we are going to create an extra column in this method.

import pandas as pd
df = pd.DataFrame({
    'cola': [100, 200, 300, 400, 500],
    'colb': ['A', 'B', 'C', 'D', 'E'],
    'colc': [True, False, True, True, False],
})
print("Existing DataFrame: ")
print(df)
# Creating a new column with single value
df['cold'] = "10"
print("Modified DataFrame: ")
print(df)

Output:

     cola   colb  colc
0    100    A     True
1    200    B     False
2    300    C     True
3    400    D     True
4    500    E     False

Data frame having a column with single values:
    cola  colb  colc  cold
0   100   A     True   10
1   200   B     False  10
2   300   C     True   10
3   400   D     True   10
4   500   E     False  10

Conclusion

With that, we come to the end of this tutorial. Please feel free to drop in your queries and doubts. Please stay tuned and subscribe for more interesting discussions and articles.

Article By: Rashi Agarwal and Shubham Sayon


Learn Pandas the Fun Way by Solving Code Puzzles

If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

Coffee Break Pandas Book

It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster?

Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience.