How to get a value from a cell of a Pandas DataFrame

  1. iloc to get value from a cell of a Pandas dataframe
  2. iat and at to get value from a cell of a Pandas dataframe
  3. df['col_name'].values[] to get value from a cell of a Pandas dataframe

We will introduce the method to get a value from a cell of a Pandas dataframe, using iloc which is good for selection by position and how it is different from loc. We will also learn iat and ['col_name'].values[] method which we use when we don’t want to get the return type as pandas.series.

iloc to get value from a cell of a Pandas dataframe

iloc is the most efficient way to get a value from the cell of a Pandas dataframe. Let’s say, we have a dataframe with the columns’ name as price and stock, and want to get a value from the 3rd row to check the price and stock availability.

First, we need to access rows and then the value using the column name.

Example Codes:

# python 3.x
import pandas as pd
df = pd.DataFrame({
    'name':
    ['orange','banana','lemon','mango','apple'],
    'price':
    [2,3,7,21,11],
    'stock':
    ['Yes','No','Yes','No','Yes']
})
print(df.iloc[2]['price'])
print(df.iloc[2]['stock'])

Output:

7
Yes

iloc gets rows (or columns) at particular positions in the index. That’s why it only takes an integer as the argument. Then loc gets rows (or columns) with particular labels from the index.

iat and at to get value from a cell of a Pandas dataframe

iat and at is fast access for scalars to get value from a cell of a Pandas Dataframe.

Example Codes:

# python 3.x
import pandas as pd
df = pd.DataFrame({
    'name':
    ['orange','banana','lemon','mango','apple'],
    'price':
    [2,3,7,21,11],
    'stock':
    ['Yes','No','Yes','No','Yes']
})
print(df.iat[0,0])
print(df.at[1,'stock'])

Output:

orange
No

To get the last row entry we will use at[df.index[-1],'stock'].

Example Codes:

# python 3.x
import pandas as pd
df = pd.DataFrame({
    'name':
    ['orange','banana','lemon','mango','apple'],
    'price':
    [2,3,7,21,11],
    'stock':
    ['Yes','No','Yes','No','Yes']
})
print(df.at[df.index[-1],'stock'])

Output:

Yes

df['col_name'].values[] to get value from a cell of a Pandas dataframe

df['col_name'].values[] will first convert datafarme column into 1-D array then access the value at index of that array:

Example Codes:

# python 3.x
import pandas as pd
df = pd.DataFrame({
    'name':
    ['orange','banana','lemon','mango','apple'],
    'price':
    [2,3,7,21,11],
    'stock':
    ['Yes','No','Yes','No','Yes']
})
print(df['stock'].values[0])

Output:

Yes

It does not return a pandas.series, and it’s the simplest to use.

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to add header row to a pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to delete a row based on column value in Pandas DataFrame
  • comments powered by Disqus