What is pandas.at() Method in Pandas?
Learn via video courses
The Pandas at() function is one of the most frequently used functions that returns us the data located at the provided location. The Pandas at() function takes the location of data present in the DataFrame in the format - DataFrame.at[position, column-name].
We have another similar function in the Pandas function, i.e., loc(), which is used to retrieve any particular value from a row or column using index values. The Pandas at() function is faster than the Pandas loc() function. Since the at() function returns a single value, it is faster than the loc() function.
The Pandas library also provides us with a property that helps us to access the single value using the row or column pair.
Syntax
The syntax of the Pandas at() function is quite simple. We just pass the row position and the column label to the function.
The syntax is:
Parameters
The Pandas at() method takes two parameters, namely-
- row-index: It tells the function the index value of the row that is to be searched.
- column-name: It tells the function of the column label in which the provided row has to be looked for.
Return Value
The Pandas at() function returns the single element present at the provided position. The at() function will take the intersection value and then returns the element present at the generated position.
Errors
The Pandas at() function raises the Key Error if the provided row index is out of the bound. Similarly, if the provided column label is not found or out of the bound, it raises the Key Error.
Examples
Let us take some examples to understand the Pandas at() function better.
1. Getting and setting the value using the at() function in Pandas.
Let us first create a DataFrame and then access a certain position using the at() function. We will then change the value of a position using the same at() function. The getting and setting feature of the at() function makes it similar to getters and setters functions.
Code:
Output:
2. Key Error generated due to out of bound.
Let us first create a DataFrame and then access a certain position using the at() function. We will then try to access a certain position that is not present in the dataFrame. So, the Pandas at() function will raise an Error.
Code:
Output:
Similarly, if we try to access a column that is not present, the Key error is generated.
Code:
Output:
Conclusion
- The Pandas at() function is one of the most frequently used functions that return us the data located at the provided location.
- The Pandas at() function takes the location of data present in the DataFrame in the format - DataFrame.at[position, column-name].
- The Pandas library also provides us with the property which helps us to access the single value using the row or column pair.
- The Pandas at() method takes two parameters, namely- row-index, which tells the function the index value of the row that is to be searched, and the column-name, which tells the function the column-label in which the provided row has to be looked for.
- The Pandas at() function returns the single element present at the provided position. The Pandas at() function raises the Key Error if the provided row index is out of the bound. Similarly, if the provided column label is not found or out of the bound, it raises the Key Error.