To explore the different ways to retrieve a single value from a pandas DataFrame, we'll be looking at a DataFrame of different NBA basketball teams.
import pandas as pd\n \n# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:\ndf = pd.DataFrame([\n {'team': 'Chicago Bulls', 'state': 'Illinois', 'valuation': 3650000000, 'championships': 6},\n {'team': 'Golden State Warriors', 'state': 'California', 'valuation': 5600000000, 'championships': 6},\n {'team': 'Los Angeles Lakers', 'state': 'California', 'valuation': 5500000000, 'championships': 17},\n {'team': 'Boston Celtics', 'state': 'Massachusetts', 'valuation': 3100000000, 'championships': 17},\n {'team': 'Miami Heat', 'state': 'Florida', 'valuation': 1750000000, 'championships': 3},\n])\n \n# Set the row label indexes to use the "team" column:\ndf.set_index('team', inplace = True)\ndf
Reset CodeRun All to Here Python Output:
state
valuation
championships
team
Chicago Bulls
Illinois
3650000000
6
Golden State Warriors
California
5600000000
6
Los Angeles Lakers
California
5500000000
17
Boston Celtics
Massachusetts
3100000000
17
Miami Heat
Florida
1750000000
3
Retrieve a Single Value using at
A common function used to retrieve single values from a DataFrame is the at function, which uses a label pair of row and column to specify the cell.
This function only accepts row labels (e.g. Chicago Bulls) as its input. This means that integers (e.g. 0) are treated as row labels, not index positions. For this function, the row label must come first, followed by the column label.
import pandas as pd\n \n# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:\ndf = pd.DataFrame([\n {'team': 'Chicago Bulls', 'state': 'Illinois', 'valuation': 3650000000, 'championships': 6},\n {'team': 'Golden State Warriors', 'state': 'California', 'valuation': 5600000000, 'championships': 6},\n {'team': 'Los Angeles Lakers', 'state': 'California', 'valuation': 5500000000, 'championships': 17},\n {'team': 'Boston Celtics', 'state': 'Massachusetts', 'valuation': 3100000000, 'championships': 17},\n {'team': 'Miami Heat', 'state': 'Florida', 'valuation': 1750000000, 'championships': 3},\n])\n \n# Set the row label indexes to use the "team" column:\ndf.set_index('team', inplace = True)\n# 'Chicago Bulls' is the row label and 'valuation' is the column label\ndf.at['Chicago Bulls', 'valuation']
Reset CodeRun All to Here Python Output:
3650000000
In addition to getting values, we can use the at function to change the value stored in a cell.
For example, the Golden State Warriors won the 2021-2022 NBA Championship, and the team now has 7 championship titles:
import pandas as pd\n \n# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:\ndf = pd.DataFrame([\n {'team': 'Chicago Bulls', 'state': 'Illinois', 'valuation': 3650000000, 'championships': 6},\n {'team': 'Golden State Warriors', 'state': 'California', 'valuation': 5600000000, 'championships': 6},\n {'team': 'Los Angeles Lakers', 'state': 'California', 'valuation': 5500000000, 'championships': 17},\n {'team': 'Boston Celtics', 'state': 'Massachusetts', 'valuation': 3100000000, 'championships': 17},\n {'team': 'Miami Heat', 'state': 'Florida', 'valuation': 1750000000, 'championships': 3},\n])\n \n# Set the row label indexes to use the "team" column:\ndf.set_index('team', inplace = True)\n# 'Golden State Warriors' is the row label and 'championships' is the column label\ndf.at['Golden State Warriors', 'championships'] = 7\ndf
Reset CodeRun All to Here Python Output:
state
valuation
championships
team
Chicago Bulls
Illinois
3650000000
6
Golden State Warriors
California
5600000000
7
Los Angeles Lakers
California
5500000000
17
Boston Celtics
Massachusetts
3100000000
17
Miami Heat
Florida
1750000000
3
Now, the Golden State Warriors' championships column contains the value 7 instead of 6
Retrieve a Single Value using iat
Another way to retrieve a single value from a pandas DataFrame is to use the iat function.
The iat function is similar to the at function, except that it only accepts integers indexes (e.g. 0, 1...) as its input.
import pandas as pd\n \n# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:\ndf = pd.DataFrame([\n {'team': 'Chicago Bulls', 'state': 'Illinois', 'valuation': 3650000000, 'championships': 6},\n {'team': 'Golden State Warriors', 'state': 'California', 'valuation': 5600000000, 'championships': 6},\n {'team': 'Los Angeles Lakers', 'state': 'California', 'valuation': 5500000000, 'championships': 17},\n {'team': 'Boston Celtics', 'state': 'Massachusetts', 'valuation': 3100000000, 'championships': 17},\n {'team': 'Miami Heat', 'state': 'Florida', 'valuation': 1750000000, 'championships': 3},\n])\n \n# Set the row label indexes to use the "team" column:\ndf.set_index('team', inplace = True)\n# 4 is the row index position and 2 is the column index position\ndf.iat[4, 2]
Reset CodeRun All to Here Python Output:
3
Retrieve a Single Value using loc
We can also find a single cell value using the loc function to specify the row using a label followed by the column label enclosed in brackets ([]):
import pandas as pd\n \n# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:\ndf = pd.DataFrame([\n {'team': 'Chicago Bulls', 'state': 'Illinois', 'valuation': 3650000000, 'championships': 6},\n {'team': 'Golden State Warriors', 'state': 'California', 'valuation': 5600000000, 'championships': 6},\n {'team': 'Los Angeles Lakers', 'state': 'California', 'valuation': 5500000000, 'championships': 17},\n {'team': 'Boston Celtics', 'state': 'Massachusetts', 'valuation': 3100000000, 'championships': 17},\n {'team': 'Miami Heat', 'state': 'Florida', 'valuation': 1750000000, 'championships': 3},\n])\n \n# Set the row label indexes to use the "team" column:\ndf.set_index('team', inplace = True)\n# 'Boston Celtics' is the row label and 'state' is the column label\n# using the loc function\ndf.loc['Boston Celtics']['state']
Reset CodeRun All to Here Python Output:
Massachusetts
Retrieve a Single Value using iloc
We can also use integer positions instead of labels to specify the row. To do this, we would use the iloc function:
import pandas as pd\n \n# Creates a DataFrame with 'state', 'valuation', and 'championships' columns:\ndf = pd.DataFrame([\n {'team': 'Chicago Bulls', 'state': 'Illinois', 'valuation': 3650000000, 'championships': 6},\n {'team': 'Golden State Warriors', 'state': 'California', 'valuation': 5600000000, 'championships': 6},\n {'team': 'Los Angeles Lakers', 'state': 'California', 'valuation': 5500000000, 'championships': 17},\n {'team': 'Boston Celtics', 'state': 'Massachusetts', 'valuation': 3100000000, 'championships': 17},\n {'team': 'Miami Heat', 'state': 'Florida', 'valuation': 1750000000, 'championships': 3},\n])\n \n# Set the row label indexes to use the "team" column:\ndf.set_index('team', inplace = True)\n# 0 is the row index and 'championships' is the column label\n# using the iloc function\ndf.iloc[0]['championships']
Reset CodeRun All to Here Python Output:
6
Pandas Documentation
Click Here for the full pandas documentation for the at function Click Here for the full pandas documentation for the iat function