JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Replace column values in Pandas DataFrame

Author:JIYIK Last Updated:2025/05/01 Views:

In this tutorial post, we will look at how to replace column values ​​in a Pandas DataFrame. We will look at three different functions to easily replace column values.


map()Replace column values ​​in Pandas using method

The columns of DataFrame are Pandas Series. We can use mapthe method to replace each value in the column with another value.

Series.map()grammar

Series.map(arg, na_action=None)
  • parameter:
  1. arg: This parameter is used to map a Series. It can be a collection or a function.
  2. na_action: na_actionis used to handle NaN(non-numeric) values. It can take two values ​​- Noneor ignore. Noneis the default value and map()will apply the mapping to all values, including Nanthe value; the value ignorewill be NaNleft in the column and not passed to the mapping method.

It returns a with the same index Series.

Now let us take an example to implement mapthe method. We will use the same in the following examples DataFrame.

import pandas as pd
import numpy as np

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "city": ["berlin", "paris", "roma", np.nan],
}
df = pd.DataFrame(data, columns=["name", "city"])

print(df)

Output:

      name    city
0  michael  berlin
1    louis   paris
2     jack    roma
3  jasmine     NaN

Replace Column Values ​​with a Set in Pandas DataFrame

import pandas as pd
import numpy as np

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "city": ["berlin", "paris", "roma", np.nan],
}
df = pd.DataFrame(data, columns=["name", "city"])

# replace column values with collection

df["city"] = df["city"].map(
    {"berlin": "dubai", "paris": "moscow", "roma": "milan", np.nan: "NY"},
    na_action=None,
)

print(df)

Output:

      name    city
0  michael   dubai
1    louis  moscow
2     jack   milan
3  jasmine      NY

The column values ​​in the original DataFrame cityare replaced with the new values ​​in the dictionary given as map()the first argument to the method.

Replace Column Values ​​with a Function in Pandas DataFrame

import pandas as pd
import numpy as np

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "city": ["berlin", "paris", "roma", np.nan],
}
df = pd.DataFrame(data, columns=["name", "city"])

# replace column values with function

df["city"] = df["city"].map("I am from {}".format)

print(df)

Output:

      name              city
0  michael  I am from berlin
1    louis   I am from paris
2     jack    I am from roma
3  jasmine     I am from nan

na_actionThe default is None, so the original column NaNis also replaced by the new string I am from nan.

If you want to keep NaNbut not replace you can na_actionset to ignore.

import pandas as pd
import numpy as np

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "city": ["berlin", "paris", "roma", np.nan],
}
df = pd.DataFrame(data, columns=["name", "city"])

# replace column values excluding NaN

df["city"] = df["city"].map("I am from {}".format, na_action="ignore")

print(df)

Output:

      name              city
0  michael  I am from berlin
1    louis   I am from paris
2     jack    I am from roma
3  jasmine               NaN

locReplace column values ​​in Pandas using method

Another way to replace Pandas DataFrame column values ​​is the replace method DataFramein loc(), loc()which accesses the value by its label.

DataFrame.loc[]grammar

pandas.DataFrame.loc[condition, column_label] = new_value
  • parameter:
  1. condition: This parameter returns the value that makes the condition true.
  2. column_label: This parameter is used to specify the target column to be updated.

After determining the value through the parameters, we update it to new_value.

Now let's take an example to implement locthe method. We will take the following DataFrameas an example.

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "grades": [30, 70, 40, 80],
    "result": ["N/A", "N/A", "N/A", "N/A"],
}

df = pd.DataFrame(data, columns=["name", "grades", "result"])

print(df)

Output:

      name  grades result
0  michael      30    N/A
1    louis      70    N/A
2     jack      40    N/A
3  jasmine      80    N/A

Replace column values ​​with condition in Pandas DataFrame

We can use Boolean conditions to specify the target element.

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "grades": [30, 70, 40, 80],
    "result": ["N/A", "N/A", "N/A", "N/A"],
}

df = pd.DataFrame(data, columns=["name", "grades", "result"])

df.loc[df.grades > 50, "result"] = "success"

df.loc[df.grades < 50, "result"] = "fail"

print(df)

Output:

      name  grades   result
0  michael      30     fail
1    louis      70  success
2     jack      40     fail
3  jasmine      80  success

df.loc[df.grades>50, 'result']='success'If gradesthe value of is greater than 50, the value sucessis replaced with .

df.loc[df.grades<50,'result']='fail'If gradesthe value is less than 50, the value failis replaced with .


Use replace()the method to modify the value

Another way to replace column values ​​in a Pandas DataFrame is Series.replace()the replace method.

Series.replace()grammar

  • Replace a single value
df[column_name].replace([old_value], new_value)
  • Replace multiple values ​​with the same value
df[column_name].replace([old_value1, old_value2, old_value3], new_value)
  • Replace multiple values ​​with multiple values
df[column_name].replace(
    [old_value1, old_value2, old_value3], [new_value1, new_value2, new_value3]
)
  • Replace a value with a new value throughout the DataFrame.
df.replace([old_value], new_value)

We will use the following DataFrame in the remaining examples.

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "salary": [700, 800, 1000, 1200],
}

df = pd.DataFrame(data, columns=["name", "salary"])

print(df)

Output:

      name  salary
0  michael     700
1    louis     800
2     jack    1000
3  jasmine    1200

Replace column values ​​with multiple values ​​in Pandas DataFrame

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "salary": [700, 800, 1000, 1200],
}

df = pd.DataFrame(data, columns=["name", "salary"])

df["name"] = df["name"].replace(["michael", "louis"], ["karl", "lionel"])

print(df)

Output:

      name  salary
0     karl     700
1   lionel     800
2     jack    1000
3  jasmine    1200

Replace column values ​​with only identical values ​​in Pandas DataFrame

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "salary": [700, 800, 1000, 1200],
}

df = pd.DataFrame(data, columns=["name", "salary"])

df["salary"] = df["salary"].replace([1000, 1200], 1500)

print(df)

Output:

      name  salary
0     karl     700
1   lionel     800
2     jack    1500
3  jasmine    1500

Replace column values ​​with a value in Pandas DataFrame

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "salary": [700, 800, 1000, 1200],
}

df = pd.DataFrame(data, columns=["name", "salary"])

df["salary"] = df["salary"].replace([700], 750)

print(df)

Output:

      name  salary
0     karl     750
1   lionel     800
2     jack    1000
3  jasmine    1200

Replace values ​​in an entire Pandas DataFrame

import pandas as pd

data = {
    "name": ["michael", "louis", "jack", "jasmine"],
    "salary": [700, 800, 1000, 1000],
}

df = pd.DataFrame(data, columns=["name", "salary"])


df = df.replace([1000], 1400)

print(df)

Output:

      name  salary
0     karl     750
1   lionel     800
2     jack    1400
3  jasmine    1400

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial