Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a single value as a string from pandas data frame

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

like image 632
Oamar Kanji Avatar asked Nov 12 '18 04:11

Oamar Kanji


People also ask

How do I pull a specific value from a pandas DataFrame?

get_value() function is used to quickly retrieve the single value in the data frame at the passed column and index. The input to the function is the row label and the column label.

How do you access a single element from a DataFrame?

You can access a single value from a DataFrame in two ways. Method 1: DataFrame.at[index, column_name] property returns a single value present in the row represented by the index and in the column represented by the column name. Method 2: Or you can use DataFrame.

How do you select a specific value in a DataFrame?

Select Data Using Location Index (. This means that you can use dataframe. iloc[0:1, 0:1] to select the cell value at the intersection of the first row and first column of the dataframe. You can expand the range for either the row index or column index to select more data.

How do I find a specific string in a DataFrame?

Using “contains” to Find a Substring in a Pandas DataFrame The contains method in Pandas allows you to search a column for a specific substring. The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not.


3 Answers

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).

like image 194
cs95 Avatar answered Sep 29 '22 03:09

cs95


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' 
like image 20
Karn Kumar Avatar answered Sep 29 '22 01:09

Karn Kumar


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]
like image 34
PL200 Avatar answered Sep 29 '22 01:09

PL200