Pandas Set Value to Particular Cell in DataFrame Using Index

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

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 the quick examples.

# 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.


# Create DataFrame
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.


# 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.


# 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.


# 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) 

5. 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 !!

References

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

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