Pandas Set Value to Particular Cell in DataFrame Using Index

You can set/change the value to a particular cell in a pandas DataFrame for a given Index by using DataFrame.at[] and DataFrame.loc[] methods. Setting a value to a cell modifies the DataFrame with a new value at some index. In this article, I will explain how to set values for a particular cell in DataFrame using an index with examples.

1. Quick Examples of Set Value for Particular Cell in DataFrame using index

If you are in a hurry, below are some quick examples of how to set/change the value for a particular cell in DataFrame using an index.


# Below are quick example

# Use DataFrame.at[] method to set value for particular cell
df.at[0,'Courses'] = 'Java'
df.at[1,'Fee'] = 40000
df.at[2,'Duration'] = '55days'

# Use Dataframe.loc[] method to set value for particular cell 
df.loc[0, 'Duration'] = "45days"
df.loc[1, 'Courses'] ="Python"
df.loc[3, 'Fee'] = 45000

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = [
            ("Spark", 22000,'40days',1500),
            ("PySpark",25000,'50days',3000),
            ("Hadoop",23000,'30days',2500),
            ("Pandas",30000,'60days',2800)
            ]
df = pd.DataFrame(technologies,columns = ['Courses','Fee','Duration','Discount'])
print(df)   

Yields below output.


   Courses    Fee Duration  Discount
0    Spark  22000   40days      1500
1  PySpark  25000   50days      3000
2   Hadoop  23000   30days      2500
3   Pandas  30000   60days      2800

2. Use DataFrame.at[] to Set Value for Particular Cell

pandas.DataFrame.at[] method is primarily used when you need to set a single value in pandas DataFrame. while accessing the cell you have to specify the index and column as DataFrame.at[0,'Courses']. This example changes the value of the Courses column of the first row. The first parameter is the index, and the second is the column.


# Use DataFrame.at[] method to set value for particular cell
df.at[0,'Courses'] = 'Java'
df.at[1,'Fee'] = 40000
df.at[2,'Duration'] = '55days'
df.at[3,'Discount'] = 4000
print(df)   

Yields below output.


   Courses    Fee Duration  Discount
0     Java  22000   40days      1500
1  PySpark  40000   50days      3000
2   Hadoop  23000   55days      2500
3   Pandas  30000   60days      4000

3. Use Dataframe.loc[] Method to Set Value for Particular Cell using index

You can also use DataFrame.loc[] method to set value for particular cell. For instance, DataFrame.loc[row_index,col_indexer]=value.


# Use Dataframe.loc[] method to set value for particular cell 
df.loc[1, 'Courses'] ="Python"
df.loc[3, 'Fee'] = 45000
df.loc[0, 'Duration'] = "45days"
df.loc[2, 'Discount'] = 2500
print(df) 

Yields below output.


  Courses    Fee Duration  Discount
0   Spark  22000   45days      1500
1  Python  25000   50days      3000
2  Hadoop  23000   30days      2500
3  Pandas  45000   60days      2800

4. Complete Example For Set Value for Particular Cell in DataFrame using index


import pandas as pd
technologies = [
            ("Spark", 22000,'40days',1500),
            ("PySpark",25000,'50days',3000),
            ("Hadoop",23000,'30days',2500),
            ("Pandas",30000,'60days',2800)
            ]
df = pd.DataFrame(technologies,columns = ['Courses','Fee','Duration','Discount'])
print(df) 

# Use DataFrame.at[] method to set value for particular cell
df.at[0,'Courses'] = 'Java'
df.at[1,'Fee'] = 40000
df.at[2,'Duration'] = '55days'
df.at[3,'Discount'] = 4000

# Use Dataframe.loc[] method to set value for particular cell 
df.loc[1, 'Courses'] ="Python"
df.loc[3, 'Fee'] = 45000
df.loc[0, 'Duration'] = "45days"
df.loc[2, 'Discount'] = 2500
print(df) 

Conclusion

In this article, you have learned how to set/change/update a value for particular cell in pandas DataFrame using index by using DataFrame.at[] and DataFrame.loc[] methods with examples

Happy Learning !!

You May Also Like

References

pandas set value cell

Leave a Reply

You are currently viewing Pandas Set Value to Particular Cell in DataFrame Using Index