- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access a single value in pandas Data Frame using the .at attribute?
The pandas DataFrame.at attribute is used to access a single value using the row and column labels. The “at” attribute takes a row and column labels data to get an element from a specified label position of the given DataFrame object.
It will return a single value based on the row and column label, and we can also upload a value in that particular position.
The .at attribute will raise a KeyError if the specified label is not available in the DataFrame.
Example 1
In this following example, we have created a Pandas DataFrame using a python dictionary. The column name is labeled by using the keys in the dictionary and the indexes are auto-generated values from 0 to n-1.
# importing pandas package import pandas as pd # create a Pandas DataFrame df = pd.DataFrame({'A':[1, 2, 3],'B':[7, 8, 6],"C":[5, 6, 2]}) print("DataFrame:") print(df) # Access a single value from the DataFrame result = df.at[0, 'B'] print("Output:",result)
Output
The output is given below −
DataFrame: A B C 0 1 7 5 1 2 8 6 2 3 6 2 Output: 7
We can see both initialized series object and output for at attribute in the above block. The .at attribute returns 7 for the following row/column pair df.at[0, 'B'].
Example 2
Now let’s update the value “100” in the position [2, 'B'] of the DataFrame object using the at attribute, 2 means the row index and “B” means column name.
# importing pandas package import pandas as pd # create a Pandas DataFrame df = pd.DataFrame({'A':[1, 2, 3],'B':[7, 8, 6],"C":[5, 6, 2]}) print("DataFrame:") print(df) # by using .at attribute update a value df.at[2, 'B'] = 100 print("Value 100 updated:") print(df)
Output
The output is as follows −
DataFrame: A B C 0 1 7 5 1 2 8 6 2 3 6 2 Value 100 updated: A B C 0 1 7 5 1 2 8 6 2 3 100 2
We have successfully updated the value “100” in the last row of the middle column (2, B), we can see the updated DataFrame object in the above output block.