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.
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.