Pandas dataframe.get_value



The get_value() method in the Pandas library is used to retrieve a scalar value from a DataFrame. This method can be used to access a specific element in a DataFrame based on its row and column index.

Syntax: DataFrame.get_value(index, col, takeable=False)

Parameters :
index : row label
col : column label
takeable : interpret the index/col as indexers, default False

Returns : value : scalar value

Let’s say we have the following DataFrame:

import pandas as pd

data = {'Name': ['John', 'Jane', 'Bob', 'Sara'],
'Age': [25, 18, 21, 32],
'Country': ['USA', 'Canada', 'UK', 'Australia']}

df = pd.DataFrame(data)

print(df)

This DataFrame looks like this:

Name Age Country
0 John 25 USA
1 Jane 18 Canada
2 Bob 21 UK
3 Sara 32 Australia

We can use the get_value() method to retrieve a specific value from the DataFrame. For example, to retrieve the value in the first row and second column (which is ’25’), we can do the following:

value = df.get_value(0, 'Age')
print(value)

This will output:

25

We can also retrieve multiple values by passing in a list of index values and a list of column values. For example, to retrieve the values in the first and third rows of the ‘Name’ and ‘Country’ columns, we can do the following:

values = df.get_value([0, 2], ['Name', 'Country'])
print(values)

Output:

0 John Doe 30

1 Jane Doe 25

2 Bob Smith 40

3 Alice Brown 35

Let’s say we want to access the value in the “age” column for the second row (i.e., “25”). We can use the get_value() method to do this as follows:

df.get_value(1, 'age')

This will return the value “25”. Here, the first argument specifies the index of the row, and the second argument specifies the name of the column.

It’s worth noting that the get_value() method has been deprecated since pandas version 0.21.0 and will be removed in a future version. Instead, you should use the at[] and iat[] methods to access scalar values. The at[] method is label-based, and the iat[] method is integer-based.

Here’s an example of how to use the at[] method to access the same value we accessed earlier:

df.at[1, 'age']

This will also return the value “25”. Note that the arguments are the same as for the get_value() method.

Similarly, we can use the iat[] method to access scalar values by passing in the integer location of the row and column. Here’s an example:

df.iat[1, 1]

This will also return the value “25”. Note that the arguments are the same as for the get_value() method and at[] method.

Example #1:

import pandas as pd

data = {'Name': ['John', 'Alex', 'Sarah'],
        'Age': [25, 30, 35],
        'Gender': ['Male', 'Male', 'Female']}

df = pd.DataFrame(data)

# Get the value of the first row and 'Name' column
name = df.get_value(0, 'Name')
print(name)  # Output: John

# Get the value of the second row and 'Age' column
age = df.get_value(1, 'Age')
print(age)  # Output: 30

Example #2:

import pandas as pd

data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}

df = pd.DataFrame(data)

# Get the value of the first row and second column
value = df.get_value(0, 'B')
print(value)  # Output: 4

# Set the value of the second row and third column to 10
df.set_value(1, 'C', 10)

# Get the value of the second row and third column
value = df.get_value(1, 'C')
print(value)  # Output: 10

Example #3:

import pandas as pd

data = {'Country': ['USA', 'Canada', 'Mexico'],
        'Capital': ['Washington D.C.', 'Ottawa', 'Mexico City'],
        'Population (millions)': [328, 37, 128]}

df = pd.DataFrame(data)

# Get the value of the first row and 'Country' column
country = df.get_value(0, 'Country')
print(country)  # Output: USA

# Get the value of the third row and 'Population (millions)' column
population = df.get_value(2, 'Population (millions)')
print(population)  # Output: 128

These examples demonstrate how the get_value() method can be used to retrieve individual values from a Pandas DataFrame. The method provides a simple and efficient way to access and manipulate data within a DataFrame.

Last Updated on June 15, 2023 by admin

2 thought on “Pandas dataframe.get_value”

  1. Gabriel says:

    import pandas as pd
    df = pd.read_csv(“nba.csv”)
    df.get_value(10, ‘Salary’)

    AttributeError Traceback (most recent call last)
    ~\AppData\Local\Temp/ipykernel_8/2482875481.py in
    4 df = pd.read_csv(“nba.csv”)
    5 # applying get_value() function
    —-> 6 df.get_value(10, ‘Salary’)

    ~\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
    5485 ):
    5486 return self[name]
    -> 5487 return object.__getattribute__(self, name)
    5488
    5489 def __setattr__(self, name: str, value) -> None:

    AttributeError: ‘DataFrame’ object has no attribute ‘get_value’

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs