How to access a single value in a row/column label of a DataFrame
Overview
We can access a single value for a row or column label of a DataFrame in Pandas with the help of the at attribute.
The at attribute is used to obtain a single value in a given DataFrame or Series.
Syntax
The at attribute takes the syntax below.
DataFrame.at
Syntax for the "at" attribute in Pandas
Parameters
As an attribute, at does not take a parameter value. It takes the index row and column labels of the desired value in the DataFrame.
Return value
The at attribute returns a single value in a DataFrame or Series.
Example
import pandas as pd# creating a list of objectsint_values = [1, 2, 3, 4, 5]text_values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']float_values = [0.0, 0.25, 0.5, 0.75, 1.0]# creating a dataframe from the list of objectsdf = pd.DataFrame({"int_column": int_values, "text_column": text_values,"float_col": float_values})# printing the dataframeprint(df)# accessing the value "delta" present in the 4th row and in the "text_columnm" columna = df.at[3, "text_column"]print(a)
Explanation
- Line 1: We import the
pandaslibrary. - Lines 4–6: We create a list of objects,
text_values,int_values, andfloat_values. - Lines 9–10: We create a DataFrame using the list of objects we created by using
pandas.DataFrame(). The name of the DataFrame isdf. - Line 13: We print the DataFrame,
df. - Line 16: We access the value in the DataFrame,
"delta", which is found in the 4th row (index3) and on the"text_column"column. The value is passed to a variable,a. - Line 17: We print the value of
a.