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
dataFrame['Name']
returns apd.Series
dataFrame['Name'] == 'rasberry'
returns apd.Series
withdtype
bool
dataFrame.loc[dataFrame['Name'] == 'rasberry']
uses the booleanpd.Series
to slicedataFrame
returning apd.DataFrame
that is a subset ofdataFrame
dataFrame.loc[dataFrame['Name'] == 'rasberry']['code']
is apd.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 rowpd.Series
. - You want the element inside but at this point it's the difference between
'value'
and['value']
- If you expect the elements in the
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"