How to update column values in Python Pandas [8 examples]

In this Python tutorial, we will learn various methods to update column values in Python pandas. We’ll use some built-in functions to understand different approaches to updating column values in Python Pandas.

As a Developer, while making the Python Project I got the requirement to update column values in Python Pandas.

Here we will see:

  • How to update column values in Python Pandas by using at()
  • How to update column values in Python Pandas by using replace()
  • How to update column values in Python Pandas by using iloc()
  • How to update column values in Python Pandas by using loc()
  • How to update the case of the column names in Python Pandas
  • How to update the column values in Python Pandas by using rename()
  • How to update Dataframe with different data lengths using update()
  • How to update Dataframe at a specific location using update()

How to update column values in Python Pandas

In Python, there are primarily a few methods that are commonly used and important to understand when updating column values in Python Pandas.

How to update column values in Python Pandas by using at()

  • In this section, we will discuss how to update column values in Python Pandas by using the replace() function.
  • First, we will create a dataframe using the pd.dataframe() function, and data is stored in a data frame as rows and columns. As a result, it qualifies as a matrix and is helpful for data analysis.

Example:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
print(df)

Here is the Screenshot of the following given code:

How to update column values in Python Pandas
  • Now after creating a dataframe, we will update the column value by using the at() function
  • Based on the row index and column name, the at() method in pandas is used to extract a single value from a dataframe.
  • With the help of Python’s at() method, we can change a row’s value about a column one at a time.

Syntax:

Here is the Syntax of the dataframe.at() method in Python

Dataframe.at[rowIndex, columnLabel]

Note: This parameter takes two parameters row index and column label. If the arguments given as the row index and column labels are out of bounds or are missing from the dataframe, the key error is raised.

In this example, we have used the at() function with an index 4 of the data frame and column ‘Country_name’. Thus, the value of the column ‘Country_name’ at row index 4 gets modified.

Source Code:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
df.at[4,'Country_name']='Albania'
print(df)

Here is the implementation of the following given code.

pandas update column values in Python

This is how we can update column values in the dataframe using the at() function.

How to update column values in Python Pandas by using replace()

  • Now let us understand how to update column values in Python Pandas using replace().
  • Any string within a data frame can have its value updated or changed using the replace() function in Python. The index and label values are not necessary to be given to it.
  •  Column values can be changed using the DataFrame.replace() function (one value with another value on all columns). A new DataFrame is returned by this function, which accepts the parameters to replace, value, inplace, limit, regex, and method. When the inplace=True parameter is used, it replaces an existing DataFrame object and returns a result of None.

Syntax:

Here is the Syntax of the dataframe.replace() function in Python Pandas

DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad')
  • It consists of a few parameters
    • to_replace: Make a string, list, dictionary, regex, integer, float, or other data type, and specify the values to be replaced.
    • value: By default, it takes no value and specifies the value we want to be replaced.
    • inplace: whether to operate in place. By default, it takes a false value.
    • limit: maximum gap to fill when moving forward or backward.
    • regex: if to replace and/or value should be treated as regex.
    • method: By default, it takes the ‘pad’ value and it is used for the replacement.

Example:

Let’s take an example and check how to update column values in Python Pandas using the replace().

Source Code:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
df.replace("Australia", "Argentina", inplace=True)
print(df)

You can refer to the below Screenshot:

update dataframe column value in Python

As you can see in the Screenshot we have discussed how to update column values in Python Pandas by using the replace() function.

How to update column values in Python Pandas by using iloc()

  • In this section, we will discuss how to update column values in Python Pandas by using the iloc() function.
  • By providing the index values of the corresponding row/column, one can update or change the value of the row/column using the Python iloc() method.

Example:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
df.iloc[[1,3],[1]] = 100
print(df)
  • In this instance, we have changed the value of rows 1, 3, and the first column, “Num,” to 100.
  • Using the iloc() function, we may even slice the rows provided to the function to modify the values of many rows at once.

Here is the Screenshot of the following given code.

dataframe update column value in Python Pandas

This is how to update column values in Python Pandas by using the iloc() function.

How to update column values in Python Pandas by using loc()

  • In this section, we will discuss how to update column values in Python Pandas by using the loc() function.
  • Rows and columns of a pandas DataFrame are selected using loc. The simplicity of usage of DataFrame is one of its key benefits. When you use pandas, you can verify. DataFrame. To choose or filter DataFrame rows or columns, use the loc[] attribute.
  • By providing the labels of the columns and the index of the rows, the loc() method in Python can also be used to change the value of a row to its columns.

Syntax:

Here is the Syntax of the loc() method in Python Pandas.

dataframe.loc[row index,['column-names']] = value

Example:

Let’s take an example and check how to update column values in Python Pandas by using the loc() function.

Source Code:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
df.loc[0:2,['index','Country_name']] = [100,'Angola']
print(df)

Here is the implementation of the following given code.

pandas dataframe update column values in Python

This is how to update the column values in Python Pandas by using the loc() function.

How to update the case of the column names in Python Pandas

  • Now let us understand how to update the case of the column names in Python Pandas.
  • All of the column names in our data have their initial letter capitalized, as you can see. Having a standard case for all of your column names is always preferred.

Example:

Let’s take an example and check how to update the case of the column names in Python Pandas.

Source Code:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
df.columns = df.columns.str.lower()
print(df)

You can refer to the below Screenshot.

python dataframe update column value in pandas

In this example, we have understood how to update the case of the column names in Python Pandas.

How to update the column values in Python Pandas by using rename()

  • Now let us understand how to update the column values in Python Pandas by using the rename() function.
  • Using the rename() function is one technique to rename the columns in a Pandas Dataframe. When we need to rename a few specific columns, this approach works well because we just need to supply information for the columns that need to be changed.

Syntax:

Let’s have a look at the Syntax and understand the working of the df.rename() function in Python.

DataFrame.rename(mapper=None, *, index=None, columns=None, axis=None, copy=None, inplace=False, level=None, errors='ignore')

Example:

Here we will take an example and check how to update the column values in Python Pandas by using the rename() function.

Source Code:

import pandas as pd

new_val = {"Country_name": ['USA','Germany','Australia','China'],"Zip_Code": [1456,8723,9562,8652]}
df = pd.DataFrame(new_val, index=[1,2,3,4])
result=df.rename(columns={'Country_name': 'Country'})
print(result)

Here is the execution of the following given code.

pandas update column value in Python

This is how to update the column values in Python Pandas by using rename() function.

How to update Dataframe with different data lengths using update()

  • In this section, we will discuss how to update the Dataframe with different data lengths using the update() function.
  • Let’s consider a situation where I need to update a dataframe with more records than the original dataframe. If I use the update() function, records will be updated up until the length matches the size of the initial dataframe.

Example:

Let’s take an example and check how to update Dataframe with different data lengths using the update() function.

Source Code:

import pandas as pd

new_data = pd.DataFrame({
    'Cities_of_U.S.A': ['NewYork', 'California', 'Phenix City'],
    'new_val': [56, 18, 21]
})
print("Original Dataframe: \n", new_data)

result = pd.DataFrame({'new_val': [4, 5, 6]})
new_data.update(result)
print("Changed Dataframe:\n", new_data)

Here is the implementation of the following given code.

python pandas update column values

This is how to update Dataframe with different data lengths using an update() function.

How to update Dataframe at a specific location using update()

  • In this section, we will discuss how to update Dataframe at a specific location using update().
  • I’ll change the values of a specified location in this example. We must first create a dataframe using the index argument, and then use the update method on it.

Example:

Let’s take an example and check how to update Dataframe at a specific location using the update() function.

Source Code:

import pandas as pd

new_data = pd.DataFrame({
    'Cities_of_U.S.A': ['NewYork', 'California', 'Phenix City'],
    'new_val': [56, 18, 21]
})
print("Original Dataframe: \n", new_data)

new_result = pd.DataFrame({'new_val': [1, 2]}, index=[0, 2])
new_data.update(new_result)
print("Modified Dataframe :\n", new_data)

Here is the execution of the following given code:

pandas update values in column in Python

This is how to update Dataframe at a specific location using the update() function.

You may also like to read the following Python Pandas tutorials.

In this article, we have discussed how to update column values in Python pandas. Also, we have covered the following given topics.

  • How to update column values in Python Pandas by using at()
  • How to update column values in Python Pandas by using replace()
  • How to update column values in Python Pandas by using iloc()
  • How to update column values in Python Pandas by using loc()
  • How to update the case of the column names in Python Pandas
  • How to update the column values in Python Pandas by using rename()
  • How to update Dataframe with different data lengths using update()
  • How to update Dataframe at a specific location using update()