ADVERTISEMENT
ADVERTISEMENT

Set value for particular cell in Pandas DataFrame using index

Set value for particular cell in Pandas DataFrame using index.
Submitted by Pranit Sharma, on April 12, 2022

Pandas DataFrame allows you to store data in the form of rows and columns. Sometimes you need to manipulate this data for effective analytics. Also, sometimes you may need to set the value of particular cell using index.

Here, Index refers to the number of rows which ranges from 0 to n-1.

To set the value of a particular cell using index, you should use the following property:

Syntax:

DataFrame.at['index','column_name']

pandas.DataFrame.at Property

The at property is used to find the position of a cell value. It takes two arguments; Index number and column name and it returns the respective cell value. This method can also be used to set the value of a particular cell.

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Example:

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    "Name":['Hari','Mohan','Neeti','Shaily'],
    "Age":[25,36,26,21],
    "Gender":['Male','Male','Female','Female'],
    "Profession":['Doctor','Teacher','Singer','Student']
}

# Now, we will create DataFrame
df = pd.DataFrame(d)

# Printing the original DataFrame
print("Original DataFrame:\n")
print(df,"\n\n")

# Now, we will change the cell value
# In the name column, we will change the value 
# from Shaily to Astha
# For this we need to pass its row number i.e. 
# index and also the column name.
df.at[3,'Name'] = 'Astha'

# Now, Printing the modified DataFrame
print("Modified DataFrame:\n")
print(df)

Output:

Original DataFrame:

     Name  Age  Gender Profession
0    Hari   25    Male     Doctor
1   Mohan   36    Male    Teacher
2   Neeti   26  Female     Singer
3  Shaily   21  Female    Student 


Modified DataFrame:

    Name  Age  Gender Profession
0   Hari   25    Male     Doctor
1  Mohan   36    Male    Teacher
2  Neeti   26  Female     Singer
3  Astha   21  Female    Student

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.