Site icon Spark By {Examples}

Pandas Set Value to Particular Cell in DataFrame Using Index

pandas set value cell

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.

Key Points –

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

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

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

You can also use DataFrame.loc[] method to set values for particular cells using the index. 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) 

The above program uses the DataFrame.loc[] method to set values for specific cells based on their index and column label. It first displays the original DataFrame and then modifies particular cells using the loc[] method. Finally, it prints out the DataFrame after the modifications. This example yields the 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

Complete Example


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) 

Frequently Asked Questions on Set Value for Particular Cell in DataFrame Using Index

What is the purpose of setting a value to a particular cell in a Pandas DataFrame?

Setting a value to a particular cell allows you to modify specific data points within the DataFrame, which can be useful for data manipulation and analysis tasks.

How can I set a value to a particular cell using index and column labels in Pandas?

You can set a value to a particular cell in a Pandas DataFrame using its index and column labels with either the DataFrame.at[] or DataFrame.loc[] method.

What is the difference between DataFrame.at[] and DataFrame.loc[] methods?

The DataFrame.at[] method is specifically designed for label-based indexing, meaning it’s optimized for setting a single value using row and column labels.
The DataFrame.loc[] method is more versatile and can be used for label-based indexing as well as boolean array indexing.

Can I use integer-based indexing with DataFrame.loc[] to set values in a Pandas DataFrame?

You can use integer-based indexing with DataFrame.loc[] to set values in a Pandas DataFrame.While DataFrame.loc[] is commonly associated with label-based indexing, it also supports integer-based indexing. When using DataFrame.loc[] with integer-based indexing, it interprets the provided indexers as row labels rather than positional indices.

Is there a performance difference between using DataFrame.at[] and DataFrame.loc[] for setting values?

Generally, DataFrame.at[] is faster than DataFrame.loc[] when setting a single value to a particular cell because it’s optimized for label-based indexing. However, for more complex indexing operations, DataFrame.loc[] might be more appropriate.

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

Exit mobile version