How to update a particular cell value in pandas DataFrame
Retrieving a single cell value or setting up the single cell value of a row in pandas dataFrame is sometime required when you dont want to create a new Dataframe for just updating that single cell value. You can retrieve and updates values from DataFrame using the following methods.- .loc[]
- .iloc[]
- .at[]
- .iat[]
Create a DataFrame with values
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben']
df['TotalMarks'] = [82, 38, 63,22,55,40]
df['Grade'] = ['A', 'E', 'B','E','C','D']
df['Promoted'] = [True, False,True,False,True,True]
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
3 Jim 22 E False
4 Harry 55 C True
5 Ben 40 D True
Set a particular cell value using DataFrame.loc[]
DataFrame.loc[] - selects subsets of rows and columns by label only.
df.loc[2,'TotalMarks'] = 111
Above code update third row of 'TotalMarks' column to 111 using DataFrame.loc[] .
![Set a particular cell value using DataFrame.loc[]](img/loc-setvalue.png)
DataFrame.iloc - selects subsets of rows and columns by integer location only.
df.iloc[[2], [1]] = 222
Above code update third row of 'TotalMarks' column to 222 using DataFrame.iloc[] .
Set a particular cell value using DataFrame.iloc[]
![Set a particular cell value using DataFrame.iloc[]](img/iloc-setvalue.png)
Also, you can use .at[] or .iat[] for setting a particular cell value. But it is not recommended their usage unless you have a very time-sensitive application.
Set a particular cell value using DataFrame.at[]
DataFrame.at[ ] - selects a single scalar value in the DataFrame by label only.
df.at[2, 'TotalMarks'] = 333
Above code update third row of 'TotalMarks' column to 333 using DataFrame.at[] .
![Set a particular cell value using DataFrame.at[]](img/at-setvalue.png)
Set a particular cell value using DataFrame.iat[]
DataFrame.iat[] - selects a single scalar value in the DataFrame by integer location only.
df.iat[2, 1] = 444
Above code update third row of 'TotalMarks' column to 444 using DataFrame.iat[] .
![Set a particular cell value using DataFrame.iat[]](img/iat-setvalue.png)
SettingWithCopy warning
The crucial problem with chained indexing is when you assigning new values to the subset, in which DataFrame will usually alert the SettingWithCopy warning . This warning alert you that your DataFrame operation might not have worked as expected and that you should check the result and to make sure that you haven't made a mistake. May be you are tempting to ignore this warning if your code still works as expected. Actually, this is not a good practice and never be ignored this warning. Take some time to review your code and clarify why you are getting this warning.
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame