sovon
sovon

Reputation: 907

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,Code
blackberry,1
wineberry,2
rasberry,1
blueberry,1
mulberry,2

And here is my testing code-

dataFrame=pd.read_csv("test.csv")
value = dataFrame.loc[dataFrame['Name'] == 'rasberry']['Code']
print(value)
strvalue=str(value)
if(strvalue=="1"):
    print("got it")

The expected ouput of value would be 1 but it is

2  1\nName: Code, dtype: int64

and that's why the if condition is not working. How can I get the specific value? I am using pandas

Upvotes: 6

Views: 18472

Answers (2)

piRSquared
piRSquared

Reputation: 294198

Break It Down

  1. dataFrame['Name'] returns a pd.Series
  2. dataFrame['Name'] == 'rasberry' returns a pd.Series with dtype bool
  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)

Upvotes: 3

akuiper
akuiper

Reputation: 214927

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

Upvotes: 5

Related Questions