set value of a cell in pandas dataframe

Pandas – Set Value of Specific Cell in DataFrame

In this tutorial, we will look at how to set or update the value of a cell in a pandas dataframe with the help of some examples.

set value of a cell in pandas dataframe

You can use the .at or .iat properties to access and set value for a particular cell in a pandas dataframe. The following is the syntax:

# set value using row and column labels
df.at[row_label, column_label] = new_value
# set value using row and column integer positions
df.iat[row_position, column_position] =  new_value

The .at property lets you access the value of a single cell based on its row/column label pair whereas the .iat property lets you access the value for the cell based on its row/column integer position.

These properties are similar to the .loc and the iloc property in pandas but they are generally used to access a range of values whereas .at and iat are specifically used to access a single value.

Let’s look at some examples. First, we will create a pandas dataframe that we will be using throughout this tutorial.

import pandas as pd

# create a pandas dataframe
scores_df = pd.DataFrame({
    'Maths': [49, 81, 83],
    'History': [88, 70, 76],
    'Science': [61, 76, 90]
}, index = ['Sam', 'Soniya', 'Neeraj'])

# display the dataframe
print(scores_df)

Output:

        Maths  History  Science
Sam        49       88       61
Soniya     81       70       76
Neeraj     83       76       90

We created a dataframe with three rows, each storing the scores of a student in the subjects – Maths, History, and Science. Note that the dataframe’s rows are indexed by the student names.

On evaluating her answer scripts, Soniya found out a totaling mistake in her History scores. Her history total is actually 72. You are a TA on the course and have been tasked to update the scores accoringly.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Use the .at property to modify the value of a cell by its row and column labels.

# display the current value
print(scores_df.at['Soniya', 'History'])
# modify value by row and column label
scores_df.at['Soniya', 'History'] = 72
# display the dataframe
print(scores_df)

Output:

70
        Maths  History  Science
Sam        49       88       61
Soniya     81       72       76
Neeraj     83       76       90

Here, we use the .at property of the dataframe to access the value for the row label “Soniya” and the column label “History” and then modify it to the new value.

Alternatively, you can the dataframe .loc property to change the value by row and column labels as well. For example, let’s change the scores of Neeraj in Maths from 83 to 87.

# display the current value
print(scores_df.at['Neeraj', 'Maths'])
# modify value by row and column label
scores_df.loc['Neeraj', 'Maths'] = 87
# display the dataframe
print(scores_df)

Output:

83
        Maths  History  Science
Sam        49       88       61
Soniya     81       72       76
Neeraj     87       76       90

You can see that the “Maths” score for “Neeraj” has been modified. Note that .loc is generally used to access a range of values whereas .at only works for a single value.

Use the .iat property to modify the value of a cell by its row and column numbers (positions). For example, let’s change the scores of “Neeraj” in “Maths” from 87 to 83 using the row and column numbers.

# display the current value
print(scores_df.iat[2, 0])
# modify value by row and column numbers
scores_df.iat[2, 0] = 83
# display the dataframe
print(scores_df)

Output:

87
        Maths  History  Science
Sam        49       88       61
Soniya     81       72       76
Neeraj     83       76       90

Here, we use the .iat property of the dataframe to access the value in the row position 2 and the column position 0 and then modify it to the new value. Note that the row and column integer positions start from 0.

Alternatively, you can use the dataframe .iloc property to change the value by row and column positions as well. For example, let’s change the scores of “Neeraj” in “Maths” from 83 to 87 using its row position 2 and column position 0.

# display the current value
print(scores_df.iloc[2, 0])
# modify value by row and column positions
scores_df.iloc[2, 0] = 87
# display the dataframe
print(scores_df)

Output:

83
        Maths  History  Science
Sam        49       88       61
Soniya     81       72       76
Neeraj     87       76       90

Refer to the documentation of the .at and the .iat properties in pandas for more.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top