48

I am querying a single value from my data frame which seems to be 'dtype: object'. I simply want to print the value as it is with out printing the index or other information as well. How do I do this?

col_names = ['Host', 'Port']
df = pd.DataFrame(columns=col_names)
df.loc[len(df)] = ['a', 'b']

t = df[df['Host'] == 'a']['Port']
print(t)

OUTPUT: enter image description here

EXPECTED OUTPUT: b

6
  • 1
    Try using t = df[df['Host'] == 'a']['Port'][0] or t = df[df['Host'] == 'a']['Port'][1]. I have a fuzzy memory of this working for me during debugging in the past. Commented Nov 12, 2018 at 4:02
  • Nice, t = df[df['Host'] == 'a']['Port'][1] worked Commented Nov 12, 2018 at 4:06
  • Using .loc df.loc[df['Host'] == 'a','Port'][0] Commented Nov 12, 2018 at 4:06
  • @OamarKanji, it should be simple df[df['Host'] == 'a']['Port'][0] or `print(df[df['Host'] == 'a']['Port'][0]) Commented Nov 12, 2018 at 4:10
  • @OamarKanji Glad to have helped, I moved my comment to an answer Commented Nov 12, 2018 at 4:12

4 Answers 4

77

If you can guarantee only one result is returned, use loc and call item:

>>> df.loc[df['Host'] == 'a', 'Port'].item()
'b'

Or, similarly,

>>> df.loc[df['Host'] == 'a', 'Port'].values[0]
'b'

...to get the first value (similarly, .values[1] for the second). Which is better than df.loc[df['Host'] == 'a', 'Port'][0] because, if your DataFrame looks like this,

  Host Port
1    a    b

Then "KeyError: 0" will be thrown—

df.loc[df['Host'] == 'a', 'Port'][0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

Alternatively, use at:

>>> df.at[df['Host'].eq('a').idxmax(), 'Port']
'b'

The drawback is that if 'a' doesn't exist, idxmax will return the first index (and return an incorrect result).

Sign up to request clarification or add additional context in comments.

4 Comments

FutureWarning: 'item' has been deprecated and will be removed in a future version
@rraadd88 Are you sure? The docs don't say that here?
Yup, for some reason that warning do not exist any more. Strange.
or, you can use df.Host.values to get the list with values of Host column, or df.Host.values[0] and df.Port.values[0] to get string values. Correct me if I wrong, it works for me
9
t = df['Host'].values[0] 

will give you the first value. If you need a string, just do:

t = str(df['Host'].values[0])

Comments

1

As mentioned in my comment, using [1] should work afterwards, to pull the variable you're looking for.

t = df[df['Host'] == 'a']['Port'][1]

1 Comment

As mentioned above, regardless of whether the correct value is at the 0th or 1st position, this will not work if the index is a RangeIndex starting from 0.
1

it should work simply..

>>> df
  Host Port
0    a    b
>>> df[df['Host'] == 'a']['Port'][0]   # will choose the first index simply which is 'b'
'b'

OR, use with print which will strip off the surrounded single ticks.

>>> print(df[df['Host'] == 'a']['Port'][0])
b

This will easier because you have just choose the desired Index even if you have Multiple values across Port columns

Example:

>>> df
  Host Port
0    a    b
1    c    c

Looking for distinct a & c based on Index:

>>> df[df['Host'] == 'a']['Port'][0]
'b'
>>> df[df['Host'] == 'c']['Port'][1]
'c'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.