Skip to content Skip to sidebar Skip to footer

Selecting A Specific Value From A Data Frame

I am trying to select a value from a dataframe. But the problem is the output is with data type and column name. Here is my data frame which i am reading from a csv file, Name,Cod

Solution 1:

The value you get is a Series object. You can use .iloc to extract the value from it:

value.iloc[0]
# 1

Or you can use .values to extract the underlying numpy array and then use index to extract the value:

value.values[0]
# 1

Solution 2:

Break It Down

  1. dataFrame['Name'] returns a pd.Series
  2. dataFrame['Name'] == 'rasberry' returns a pd.Series with dtypebool
  3. dataFrame.loc[dataFrame['Name'] == 'rasberry'] uses the boolean pd.Series to slice dataFrame returning a pd.DataFrame that is a subset of dataFrame
  4. dataFrame.loc[dataFrame['Name'] == 'rasberry']['code'] is a pd.Series that is the column named 'code' in the sliced dataframe from step 3.
    • If you expect the elements in the 'Name' column to be unique, then this will be a one row pd.Series.
    • You want the element inside but at this point it's the difference between 'value' and ['value']

Setup

from io import StringIO

txt = """Name,Code
blackberry,1
wineberry,2
rasberry,1
blueberry,1
mulberry,2"""

Solution(s)

use iloc to grab first value

dataFrame=pd.read_csv(StringIO(txt))
value = dataFrame.query('Name == "rasberry"').Code.iloc[0]
print(value)

use iat to grab first value

dataFrame=pd.read_csv(StringIO(txt))
value = dataFrame.query('Name == "rasberry"').Code.iat[0]
print(value)

specify index column when reading in csv and use loc

dataFrame=pd.read_csv(StringIO(txt), index_col='Name')
value = dataFrame.loc['rasberry', 'Code']
print(value)

specify index column when reading in csv and use at

dataFrame=pd.read_csv(StringIO(txt), index_col='Name')
value = dataFrame.at['rasberry', 'Code']
print(value)

specify index column when reading in csv and use get_value

dataFrame=pd.read_csv(StringIO(txt), index_col='Name')
value = dataFrame.get_value('rasberry', 'Code')
print(value)

specify the index column when reading the csv and squeeze into a series if only one non index column exists

series=pd.read_csv(StringIO(txt), index_col='Name', squeeze=True)
value = series.rasberry
print(value)

Post a Comment for "Selecting A Specific Value From A Data Frame"