The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Set DataFrame Values – .at[], .iat[]

Set cell values in Pandas using .at[] and .iat[]

Pandas Set Values is important when writing back to your CSV. Usually you’re doing to be reading Pandas tables. But what if you’re treating a CSV like a basic database and you need to update a cell value? If you need to set/get a single DataFrame values, .at[] and .iat[] is the way to do it.

Pandas .at[] and .iat[] is similar to .loc[]. Make sure to use square brackets instead of normal function parenthesis.

Vocabulary words we must know:

  • .at[] = Using the index labels as the identifier of the intersection you’d like to update
  • .iat[] = Using the index position as the identifier of the intersection you’d like to update.
  • Intersection = The row/column combination that identifies a single cell in your DataFrame.
  • Index Label = The name of your single index. For rows this is often the row number, but this can be any other string or timestamp as well.
  • Index Position = The integer that represents the spot # your row/column sits in. You can always think of this as row/column number. Remember that python starts at index=0. So row 1 will be the 2nd row in your DataFrame.
1. pd.DataFrame.at['your_row_label', 'your_column_label']
2. pd.DataFrame.at[your_row_position, your_column_position]

Pseudo code: For a given row/column combination, get or set a value.

Pandas Set Values

.at[] and .iat[] Parameters

.at[]and.iat[] have similar but different parameters. We mostly use .at[] because it reads a bit easier.

  • .at[]: Will take a row/column intersection of index labels. The key word is labels. These are the values that actually appear on your Frame. Most often this will be your row numbers and column names. In the picture above, β€œSally” and β€œTest2” are our row labels.
  • .iat[]: Will take a row/column intersection of index positions via integers. Make sure to remember that python indexes start at 0. So your 2nd row is actually in position 1. In the picture above, row 1 and column 1 would refer to the cell that contains ’60’.

Let’s run through a few examples.

Link to code

Official Documentation

On this page