What is the difference between Pandas at and loc?

The .at and .loc attributes of Pandas are used to select rows and columns from a DataFrame. .at is used to access a single value for a row/column label pair, while .loc is used to access multiple values either by label or by boolean indexing. The .at attribute is faster than .loc as it only accesses a single value. Therefore, it is best to use .at when searching for a specific value in a DataFrame, and .loc when searching for multiple values.


When it comes to selecting rows and columns of a pandas DataFrame, .loc and .at are two commonly used functions.

Here is the subtle difference between the two functions:

  • .loc can take multiple rows and columns as input arguments
  • .at can only take one row and one column as input arguments

The following examples show how to use each function in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

Example 1: How to Use loc in Pandas

The following code shows how to use .loc to access the value in the DataFrame located at index position 0 of the points column:

#select value located at index position 0 of the points column
df.loc[0, 'points']

18

This returns a value of 18.

And the following code shows how to use .loc to access rows between index values 0 and 4 along with the columns points and assists:

#select rows between index values 0 and 4 and columns 'points' and 'assists'
df.loc[0:4, ['points', 'assists']]

        points	assists
0	18	5
1	22	7
2	19	7
3	14	9
4	14	12

Whether we’d like to access one single value or a group of rows and columns, the .loc function can do both.

Example 2: How to Use at in Pandas

The following code shows how to use .at to access the value in the DataFrame located at index position 0 of the points column:

#select value located at index position 0 of the points column
df.at[0, 'points']

18

This returns a value of 18.

However, suppose we try to use at to access rows between index values 0 and 4 along with the columns points and assists:

#try to select rows between index values 0 and 4 and columns 'points' and 'assists'
df.at[0:4, ['points', 'assists']] 

TypeError: unhashable type: 'list'

We receive an error because the at function is unable to take multiple rows or multiple columns as input arguments.

Conclusion

When you’d like to access just one value in a pandas DataFrame, both the loc and at functions will work fine.

However, when you’d like to access a group of rows and columns, only the loc function is able to do so.

Related:

How to Select Rows Based on Column Values in Pandas

x