W3docs

Set value for particular cell in pandas DataFrame using index

In pandas, you can set the value of a specific cell in a DataFrame using the at method.

In pandas, you can set the value of a specific cell in a DataFrame using the at method. The at method allows you to set the value of a cell by specifying the index of the row and column.

For example, suppose you have a DataFrame called df and you want to set the value of the cell at row index 2 and column 'B' to 5:

Set value for particular cell in pandas DataFrame using index

python— editable, runs on the server

Alternatively, you can use the iat method which allows you to set the value of a cell by specifying the index of the row and column using integer based location.

Set value for particular cell in pandas DataFrame using iat

df.iat[2, 1] = 5

You can also use the loc method which allows you to set the value of a cell by specifying the label of the row and column.

Set value for particular cell in pandas DataFrame using loc

df.loc[2, 'B'] = 5

It's worth noting that, these methods are efficient and faster than other common methods like df.ix[], df.iloc[], and df[] when it comes to setting the value of a single cell.

Please keep in mind that, these methods will raise a KeyError trigger if the index or column label does not exist.