Reputation: 907
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
Reputation: 294198
dataFrame['Name']
returns a pd.Series
dataFrame['Name'] == 'rasberry'
returns a pd.Series
with dtype
bool
dataFrame.loc[dataFrame['Name'] == 'rasberry']
uses the boolean pd.Series
to slice dataFrame
returning a pd.DataFrame
that is a subset of dataFrame
dataFrame.loc[dataFrame['Name'] == 'rasberry']['code']
is a pd.Series
that is the column named 'code'
in the sliced dataframe from step 3.
'Name'
column to be unique, then this will be a one row pd.Series
.'value'
and ['value']
from io import StringIO
txt = """Name,Code
blackberry,1
wineberry,2
rasberry,1
blueberry,1
mulberry,2"""
iloc
to grab first valuedataFrame=pd.read_csv(StringIO(txt))
value = dataFrame.query('Name == "rasberry"').Code.iloc[0]
print(value)
iat
to grab first valuedataFrame=pd.read_csv(StringIO(txt))
value = dataFrame.query('Name == "rasberry"').Code.iat[0]
print(value)
loc
dataFrame=pd.read_csv(StringIO(txt), index_col='Name')
value = dataFrame.loc['rasberry', 'Code']
print(value)
at
dataFrame=pd.read_csv(StringIO(txt), index_col='Name')
value = dataFrame.at['rasberry', 'Code']
print(value)
get_value
dataFrame=pd.read_csv(StringIO(txt), index_col='Name')
value = dataFrame.get_value('rasberry', 'Code')
print(value)
squeeze
into a series if only one non index column existsseries=pd.read_csv(StringIO(txt), index_col='Name', squeeze=True)
value = series.rasberry
print(value)
Upvotes: 3
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