at[] methodPython provides many functions to help analyze data. The Python library pandas is particularly useful for data analysis. In pandas, the at[] function is used to access one value of a row and column in data. It is used with two parameters. The first is the position of the value and a column and the second is the title of the column.
Another pandas function, loc[], works similarly, but the at[] function is more efficient and faster because it can access one value at a time.
DataFrame.at[position_of_element, title_of_column]
position_of_element: This is the position of the value and a column.title_of_column: This is the title of the column.Get the single value according to the title.
#Importing Pandas moduleimport pandas as pd#Creating dataframedata = pd.DataFrame([[0, 2, 4, 6, 8], [1, 3, 5, 7, 9], [5, 10, 15, 20, 25], [10, 20, 30, 40, 50]], columns=['First', 'Second', 'Third', 'Fourth', 'Fifth'])#Creating position_of_value and title variablesposition_of_value = 2title = 'Second'#Calling at[] methodresult = data.at[position_of_value, title]#Print the resultprint(result)
pandas module.DataFrame and assign it to data variable.result to hold the value of .at[] method applied. The element position was given as the first parameter and title of a column as the second parameter. result variable.iat[] methodThe iat[] method is used to retrieve a pair value from row and column from data given in a DataFrame. Like the at[] method, it also has two parameters; the first is the position of the element in a row and the second is the position of the element in a column.
The iloc[] also works similarly to iat[] function, but it is faster because it works on a single value at a time.
DataFrame.iat[row_element,column_element]
Row_element: This is the position of the element in a row.Column_element: This is the position of the element in a column.Get the single value of row and column.
#Importing Pandas moduleimport pandas as pd#Creating Dataframedata = pd.DataFrame([[0, 2, 4, 6, 8], [1, 3, 5, 7, 9], [5, 10, 15, 20, 25], [10, 20, 30, 40, 50]], columns=['First', 'Second', 'Third', 'Fourth', 'Fifth'])#Creating column and row variablescol_value = 3row_value = 2#Calling .iat[] methodoutput = data.iat[row_value, col_value]#Print the resultprint(output)
pandas module. DataFrame and assign it to the data variable.col_value and row_value to retrieve the value of the column and the row respectively. Next, we call the iat[] method to the values from the row and the column. result variable which contains the output.