The Pandas at[]
and iat[]
methods can be used to get and set the values of specific cells in a Pandas dataframe. The at[]
method is used to get and set values in a dataframe by label, while the iat[]
method is used to get and set values in a dataframe by integer position.
While loc
can also be used to provide label-based lookups, the at[]
method is better when you only want to get or set a single value in a dataframe or series. The iat[]
method is very similar to iloc
in that it allows you to achieve a similar thing using the integer position.
In this quick and easy tutorial, we’ll see how to use the at[]
and iat[]
methods to get and set values in a Pandas dataframe.
To get started, open a Jupyter notebook and import the Pandas library, then either import data into a Pandas dataframe, or create a dataframe of dummy values.
import pandas as pd
df = pd.DataFrame(
[('Pterophyllum altum', 3, 12.5, 13.3),
('Pterophyllum scalare', 2, 10.0, 11.0),
('Pterophyllum leopoldi', 1, 8.0, 9.0)],
columns=['species', 'age', 'length', 'weight']
)
df
species | age | length | weight | |
---|---|---|---|---|
0 | Pterophyllum altum | 3 | 12.5 | 13.3 |
1 | Pterophyllum scalare | 2 | 10.0 | 11.0 |
2 | Pterophyllum leopoldi | 1 | 8.0 | 9.0 |
First, we’ll look at the at[]
method. This method is used to get a single value from a DataFrame or Series. It takes two arguments: the row index and the column name. The row index can be an integer or a label. The column name can be a string or an integer. The at[]
method returns a single value.
To get the value of the species
column in the second row (which has an index of 1, because indices start at 0) we’d enter df.at[1, 'species']
.
df.at[1, 'species']
'Pterophyllum scalare'
You can also use the at[]
method to set or change the value of a specific dataframe cell. To do this, the syntax is very similar as the get version, but at the end you can add the value you want to set. For example, df.at[row, column] = value
.
df.at[0, 'length'] = 12.0
df
species | age | length | weight | |
---|---|---|---|---|
0 | Pterophyllum altum | 3 | 12.0 | 13.3 |
1 | Pterophyllum scalare | 2 | 10.0 | 11.0 |
2 | Pterophyllum leopoldi | 1 | 8.0 | 9.0 |
While the .at[]
method takes the row and column labels as arguments, the .iat[]
method takes the row and column numbers as arguments. For example, df.iat[0, 0]
will return the value of the cell in the first row and first column.
df.iat[1, 0]
'Pterophyllum scalare'
Just as we saw with the .at[]
method, you can also use the .iat[]
method to set the value of a cell. While the .at[]
method uses the row and column labels to select a cell, the .iat[]
method uses the row and column numbers to select a cell. The .iat[]
method is faster than the .at[]
method, but it is less flexible.
To set the length
of the the species
“Pterophyllum altum” to 15, we need to pass the values 0
for the first row, and 2
for the third column, and then assign our value at the end.
df.iat[0, 2] = 15
df
species | age | length | weight | |
---|---|---|---|---|
0 | Pterophyllum altum | 3 | 15.0 | 13.3 |
1 | Pterophyllum scalare | 2 | 10.0 | 11.0 |
2 | Pterophyllum leopoldi | 1 | 8.0 | 9.0 |
Matt Clarke, Sunday, November 27, 2022