Python | Pandas Dataframe.at []

Python Methods and Functions

Pandas at† is used to return data in a data frame at the passed location. The location passed is in the format [name, column name]. This method works similarly to Pandas loc [] but at [] is used to return only one value and is therefore faster than that.

Syntax: Dataframe.at [position, label]

Parameters:
position: Position of element in column
label: Column name to be used

Return type: Single element at passed position

To download the dataset used in the following example, click here.

In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below. 

Example # 1:
In this example the data frame is created by passing the csv url to the Pandas .read_csv () method. After that, the 2nd value in the Name column is returned using the .at [] method.

# pandas module import

import pandas as pd 

 
# reading CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

  
# create position and label variables

position = 2

label = 'Name'

 
# method call .at []

output = data.at [position, label]

 
# display

print (output)

Output:
As shown in the output image, the output can be compared and you can see that the value at the 2nd position in the name column is the same as the output. 

Notes:

  • Unlike .loc [], this method only returns one value. Therefore, dataframe.at [3: 6, label] will return an error.
  • Since this method only works for single values, it is faster than the .loc [] method.




Get Solution for free from DataCamp guru