python

Replace DataFrame column values with a specific value

A DataFrame is a two-dimensional array with labeled axes (rows and columns). We can replace the values of one or more columns in a DataFrame with a specific value. In this post, we will explain multiple methods in order to do that.

Let's create a DataFrame first

Create a DataFrame df that contains multiple columns and we have assigned multiple rows to them. We can create the DataFrame with multiple methods. We can import the CSV data as DataFrame and use it to perform operations. Here we are creating the DataFrame for a dictionary.

import pandas as pd

df = pd.DataFrame({
    "A": ["a1", "a2", "a3", "a4", "a5"],
    "B": [1, 2, 3, 4, 5],
    "C": [10, 20, 30, 40, 50]
})

print(df)

Output

╒════╤═════╤═════╤═════╕
│    │ A   │   B │   C │
╞════╪═════╪═════╪═════╡
│  0 │ a1  │   1 │  10 │
├────┼─────┼─────┼─────┤
│  1 │ a2  │   2 │  20 │
├────┼─────┼─────┼─────┤
│  2 │ a3  │   3 │  30 │
├────┼─────┼─────┼─────┤
│  3 │ a4  │   4 │  40 │
├────┼─────┼─────┼─────┤
│  4 │ a5  │   5 │  50 │
╘════╧═════╧═════╧═════╛

Solution 1: Replace DataFrame Column values using assign() function

If you want to replace the values in a Pandas DataFrame column with a specific value, you can use the assign() function. This function will replace all the values of a column in Pandas DataFrame with a given value.

Syntax

df = df.assign(column_name=value)

Code example

import pandas as pd

df = pd.DataFrame({
    "col1": ["a1", "a2", "a3", "a4", "a5"],
    "col2": [1, 2, 3, 4, 5],
    "col3": [10, 20, 30, 40, 50]
})

df = df.assign(col3=1)

print(df)

Output

╒════╤════════╤════════╤════════╕
│    │ col1   │   col2 │   col3 │
╞════╪════════╪════════╪════════╡
│  0 │ a1     │      1 │      1 │
├────┼────────┼────────┼────────┤
│  1 │ a2     │      2 │      1 │
├────┼────────┼────────┼────────┤
│  2 │ a3     │      3 │      1 │
├────┼────────┼────────┼────────┤
│  3 │ a4     │      4 │      1 │
├────┼────────┼────────┼────────┤
│  4 │ a5     │      5 │      1 │
╘════╧════════╧════════╧════════╛

Here, we are replacing all the values of the 'col3' column with value 1 using the DataFrame assign() function.

If you want to replace all the values of the 'col1' column in the DataFrame then you can use the below code.

df = df.assign(col1=2)

Output

╒════╤════════╤════════╤════════╕
│    │   col1 │   col2 │   col3 │
╞════╪════════╪════════╪════════╡
│  0 │      2 │      1 │     10 │
├────┼────────┼────────┼────────┤
│  1 │      2 │      2 │     20 │
├────┼────────┼────────┼────────┤
│  2 │      2 │      3 │     30 │
├────┼────────┼────────┼────────┤
│  3 │      2 │      4 │     40 │
├────┼────────┼────────┼────────┤
│  4 │      2 │      5 │     50 │
╘════╧════════╧════════╧════════╛

Solution 2: Using loc[] in Pandas DataFrame

If you have a Pandas DataFrame and want to replace specific values in a column, then you can use the loc[]. This allows you to specify the values that you want to replace and the new values that you want to replace them with. You can also specify if you want to replace all values or just some of them.

Code example

import pandas as pd

df = pd.DataFrame({
    "col1": ["a1", "a2", "a3", "a4", "a5"],
    "col2": [1, 2, 3, 4, 5],
    "col3": [10, 20, 30, 40, 50]
})

df.loc[:,'col3'] = 1

print(df)

Output

╒════╤════════╤════════╤════════╕
│    │ col1   │   col2 │   col3 │
╞════╪════════╪════════╪════════╡
│  0 │ a1     │      1 │      1 │
├────┼────────┼────────┼────────┤
│  1 │ a2     │      2 │      1 │
├────┼────────┼────────┼────────┤
│  2 │ a3     │      3 │      1 │
├────┼────────┼────────┼────────┤
│  3 │ a4     │      4 │      1 │
├────┼────────┼────────┼────────┤
│  4 │ a5     │      5 │      1 │
╘════╧════════╧════════╧════════╛

This code is setting all the values in the col3 column to 1.

Solution 3: Using df['column_name'] = value

A pandas DataFrame is a two-dimensional, tabular data structure with labeled axes (rows and columns). Dataframes are widely used in data science, machine learning, and other statistical programming languages.

One common data wrangling task is to replace column values with a specific value. This can be done using the df['column_name'] syntax in pandas.

For example, let's say we have a DataFrame with three columns: 'A', 'B', and 'C'. We want to replace all values in column 'B' with the value 'X'. We can do this using the following code:

df['B'] = 'X'

This will replace all values in column 'B' with the value 'X'.

Full code example

import pandas as pd

df = pd.DataFrame({
    "col1": ["a1", "a2", "a3", "a4", "a5"],
    "col2": [1, 2, 3, 4, 5],
    "col3": [10, 20, 30, 40, 50]
})

df['col3'] = '9'

print(df)

Output

╒════╤════════╤════════╤════════╕
│    │ col1   │   col2 │   col3 │
╞════╪════════╪════════╪════════╡
│  0 │ a1     │      1 │      9 │
├────┼────────┼────────┼────────┤
│  1 │ a2     │      2 │      9 │
├────┼────────┼────────┼────────┤
│  2 │ a3     │      3 │      9 │
├────┼────────┼────────┼────────┤
│  3 │ a4     │      4 │      9 │
├────┼────────┼────────┼────────┤
│  4 │ a5     │      5 │      9 │
╘════╧════════╧════════╧════════╛

Conclusion

These were some methods that can be used to change or replace the column values in Pandas DataFrame. You can use them as per your requirements. If you know more methods in order to do that you can also contribute to this title.

Was this helpful?