Pandas DataFrame is a tabular structure of rows and columns. The cell is an intersection of a specific row and column, a single data point in the DataFrame. Each cell is identified by row index and column name.
Here is the figure of individual cells in a DataFrame:
The figure above contains 12 cells arranged in 3 rows and 4 columns. If the DataFrame has existing values for each cell at the creation time, we can update or set its value again.
Here are four ways to set single or multiple cells in DataFrame:
- Using df.at[] (Fast scalar access by label)
- Using df.iat[] (Fast scalar access by position)
- Using .loc[] (Label-based)
- Using .iloc[] (Index / Position-based)
Method 1: Using df.at[]
The df.at[] property is a label-based approach that sets the value of an existing cell or sets a new value for that cell at a specified position in a DataFrame. It is fast and efficient for setting single-cell values.
For using the .at[] accessor, you need both the row index and column name as labels.
Adding cell to an existing DataFrame
You can use the .at[] accessor to append a new cell to an existing DataFrame by using the row index that does not exist.
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 12, 10]} df = pd.DataFrame(data) print(df) df.at[3, 'name'] = "Nehal" df.at[3, 'score'] = 88 df.at[3, 'subject'] = "Science" df.at[3, 'class'] = 10 print(df)
Output
You can see that we added the whole new row 3 to an existing DataFrame using the df.at[] property. We have individually added a total of four cells to the DataFrame.
Modifying an existing cell value
If the DataFrame has existing column values, it will be set with the new value.
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 12, 10]} df = pd.DataFrame(data) print(df) df.at[1, 'score'] = 34 print(df)
Output
By passing row index “1” and column name “score” to the .at[] accessor, we are setting DataFrame’s score from 90 to 34.
Method 2: Using df.iat[]
For fast scalar access by position, you can use the df.iat[] accessor. The .iat[1, 1] means we are accessing row index 1 and column index 1 of the DataFrame.
You cannot use the .iat[] accessor to add new rows or cells because .iat[] is strictly for accessing and modifying existing cells by integer-location indexing.
Modifying an existing cell value
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 12, 10]} df = pd.DataFrame(data) print(df) df.iat[1, 1] = 34 print(df)
Output
Method 3: Using .loc[]
The best way to set the cell value is to use the .loc[] accessor, which identifies the exact data point based on the row index and column name. It is a label-based approach too. The row index starts from 0.
Adding cell to an existing DataFrame
The .loc[] approach is designed to access and modify rows by label. By default, it will add the row index for you if it does not exist, which can be helpful in some cases.
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 12, 10]} df = pd.DataFrame(data) print(df) df.loc[3, 'name'] = "Nehal" df.loc[3, 'score'] = 88 df.loc[3, 'subject'] = "Social Science" df.loc[3, 'class'] = 10 print(df)
Output
Modifying an existing cell value
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 11, 10]} df = pd.DataFrame(data) print(df) # Set Ankit's score to 34 using labels df.loc[1, 'score'] = 34 print(df)
Output
We set Ankit’s score from 90 to 34. How did we find the specific cell? Well, it’s the intersection of row index 1 and the score column, which gives us a score of 90, and we updated it to 34.
Setting multiple cell values
To update multiple cell values, we must provide new values as a list of lists corresponding to each row and column being set.
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 11, 10]} df = pd.DataFrame(data) print(df) # Setting multiple cell values df.loc[[1, 2], ['name', 'subject']] = [ ["Niva", "Biology"], ["Mansi", "Sanskrit"]] print(df)
Output
In the above output, we select two row indices, 1 and 2, and also two column names, “name” and “subject.“ We set the names from Ankit, Dhaval to Niva, Mansi, and subjects Science, History to Biology, and Sanskrit.
Method 4: Using .iloc[]
The .iloc[] selection is position-based, using integer positions. The iloc[0, 0] means row index 0 and column index 0, and ultimate means the first cell value.
The .iloc[] accessor can access and modify existing rows by integer-location indexing. It cannot add new rows or cell values.
Modifying an existing cell value
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 11, 10]} df = pd.DataFrame(data) print(df) # Setting first cell value df.iloc[0, 0] = "Yogita" print(df)
Output
In the output, we set the name from “Krunal” to “Yogita”.
Setting multiple cell values
We can use .iloc[] and provide a list of values for each row to update multiple cell values for multiple columns at multiple row indices.
import pandas as pd # Sample DataFrame data = {'name': ['Krunal', 'Ankit', 'Dhaval'], 'score': [85, 90, 78], 'subject': ['Maths', 'Science', 'History'], 'class': [10, 11, 10]} df = pd.DataFrame(data) print(df) # Updating multiple columns at row indices 0 and 1 df.iloc[[0, 1], :] = [ ["Yogita", 95, "Physics", 12], # Row 0 updates ["Niva", 88, "Chemistry", 11] # Row 1 updates ] print(df)
Output
In the code above, we updated entire rows 0 and 1 by setting multiple cells, as shown in the output.
Conclusion
If you want to set a single cell value, you can use the .at[] or .iat[] properties, and for setting multiple cell values, use .loc[] or iloc[] in Pandas DataFrame.