Q:

Set value for particular cell in Pandas DataFrame using index

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Convert Index to Column in Pandas Dataframe... >>
<< How to count the NaN values in a column in Pandas ...