Pandas DataFrame at method with examples

Spread the love

The at method in Pandas is used to get a single value from a DataFrame based on the row index and column name. It is similar to loc method since they both supports label based indexing.

Syntax –

DataFrame.at[rowIndex, columnLabel]

Get a Value at a Specified row-column pair –

Let’s create a sample dataframe to illustrate it.

import pandas as pd

data = {'Name': ['Eleven','Mike','Lucas','Will','Max'],
       'Age': [18, 20, 20, 18, 19],
       'Sex': ['F', 'M', 'M', 'M', 'F'],
       'Marks': [99, 85, 82, 70, 80]}
df = pd.DataFrame(data)
df

Now, I can select a value using the at method. Let’s say you want to select Mike, for that you will write.

df.at[1, 'Name']
#output
'Mike'

Set a Value at a specified row-column pair –

We can also set a value at a specified position using the at method. Let’s change the marks of Max from 80 to 90

df.at[4, 'Marks'] = 90

Rating: 1 out of 5.

Leave a Reply