Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Set Cell Values in Pandas DataFrame

  • 13 Feb, 2025
  • Com 0
Set Cell Value of Pandas DataFrame

Pandas DataFrame is a tabular structure of rows and columns. The cell is an intersection of a specific row and column, a single data point in the DataFrame. Each cell is identified by row index and column name.

Here are four ways to set single or multiple cells in a DataFrame:

  1. Using df.at[] (Fast scalar access by label)
  2. Using df.iat[] (Fast scalar access by position)
  3. Using .loc[] (Label-based)
  4. Using .iloc[] (Index / Position-based)

Here is the figure of individual cells in a DataFrame:

Individual cells of a DataFrane

The figure above contains 12 cells arranged in 3 rows and 4 columns. If the DataFrame has existing values for each cell at the creation time, we can update or set its value again.

Method 1: Using df.at[]

The df.at[] property is a label-based approach that sets the value of an existing cell or sets a new value for that cell at a specified position in a DataFrame. It is fast and efficient for setting single-cell values.

For using the .at[] accessor, you need both the row index and column name as labels.

Adding a cell to an existing DataFrame

You can use the .at[] accessor to append a new cell to an existing DataFrame by using a row index that does not exist.

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 12, 10]}

df = pd.DataFrame(data)

print(df)

df.at[3, 'name'] = "Nehal"
df.at[3, 'score'] = 88
df.at[3, 'subject'] = "Science"
df.at[3, 'class'] = 10

print(df)

Output

Adding cell to an existing Pandas DataFrame

You can see that we added a whole new row 3 to an existing DataFrame using the df.at[] property. We have individually added a total of four cells to the DataFrame.

Modifying an existing cell value

If the DataFrame has existing column values, it will be set with the new value.

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 12, 10]}

df = pd.DataFrame(data)
print(df)

df.at[1, 'score'] = 34
print(df)

Output

Using df.at[] approach to set a cell value of DataFrame

By passing row index “1” and column name “score” to the .at[] accessor, we are setting the DataFrame’s score from 90 to 34.

Method 2: Using df.iat[]

For fast scalar access by position, you can use the df.iat[] accessor. The .iat[1, 1] means we are accessing row index 1 and column index 1 of the DataFrame.

You cannot use the .iat[] accessor to add new rows or cells because .iat[] is strictly for accessing and modifying existing cells by integer-location indexing.

Modifying an existing cell value

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 12, 10]}

df = pd.DataFrame(data)
print(df)

df.iat[1, 1] = 34
print(df)

Output

Using df.iat[] to set the cell value of DataFrame

Method 3: Using .loc[]

The best way to set the cell value is to use the .loc[] accessor, which identifies the exact data point based on the row index and column name. It is a label-based approach too. The row index starts from 0.

Adding a cell to an existing DataFrame

The .loc[] approach is designed to access and modify rows by label. By default, it will add the row index for you if it does not exist, which can be helpful in some cases.

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 12, 10]}

df = pd.DataFrame(data)

print(df)

df.loc[3, 'name'] = "Nehal"
df.loc[3, 'score'] = 88
df.loc[3, 'subject'] = "Social Science"
df.loc[3, 'class'] = 10

print(df)

Output

Adding cell to an existing DataFrame using .loc

Modifying an existing cell value

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 11, 10]}

df = pd.DataFrame(data)
print(df)

# Set Ankit's score to 34 using labels
df.loc[1, 'score'] = 34
print(df)

Output

Method 1 - Using .loc accessor

We set Ankit’s score from 90 to 34. How did we find the specific cell? Well, it’s the intersection of row index 1 and the score column, which gives us a score of 90, and we updated it to 34.

Setting multiple cell values

To update multiple cell values, we must provide new values as a list of lists corresponding to each row and column being set.

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 11, 10]}

df = pd.DataFrame(data)
print(df)

# Setting multiple cell values
df.loc[[1, 2], ['name', 'subject']] = [
    ["Niva", "Biology"], ["Mansi", "Sanskrit"]]
print(df)

Output

Setting multiple cell values

In the above output, we select two row indices, 1 and 2, and also two column names, “name” and “subject.“ We set the names from Ankit, Dhaval to Niva, Mansi, and subjects Science, History to Biology, and Sanskrit.

Method 4: Using .iloc[]

The .iloc[] selection is position-based, using integer positions. The iloc[0, 0] means row index 0 and column index 0, and ultimate means the first cell value.

The .iloc[] accessor can access and modify existing rows by integer-location indexing. It cannot add new rows or cell values.

Modifying an existing cell value

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 11, 10]}

df = pd.DataFrame(data)
print(df)

# Setting first cell value
df.iloc[0, 0] = "Yogita"
print(df)

Output

Using .iloc to set the cell value in Pandas DataFrame

In the output, we set the name from “Krunal” to “Yogita”.

Setting multiple cell values

We can use .iloc[] and provide a list of values for each row to update multiple cell values for multiple columns at multiple row indices.

import pandas as pd

# Sample DataFrame
data = {'name': ['Krunal', 'Ankit', 'Dhaval'],
        'score': [85, 90, 78],
        'subject': ['Maths', 'Science', 'History'],
        'class': [10, 11, 10]}

df = pd.DataFrame(data)
print(df)

# Updating multiple columns at row indices 0 and 1
df.iloc[[0, 1], :] = [
    ["Yogita", 95, "Physics", 12],  # Row 0 updates
    ["Niva", 88, "Chemistry", 11]   # Row 1 updates
]
print(df)

Output

Setting multiple cell values using iloc accessor in DataFrame

In the code above, we updated entire rows 0 and 1 by setting multiple cells, as shown in the output.

Conclusion

To set a single cell value, use the .at[] or .iat[] properties. For multiple cell values, use .loc[] or iloc[] in a Pandas DataFrame.

Post Views: 50
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Subtract Two Lists in Python
Adding Multiple Elements to a Set in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend