57

I have the dataframe shown below. I need to get the scalar value of column B, dependent on the value of A (which is a variable in my script). I'm trying the loc() function but it returns a Series instead of a scalar value. How do I get the scalar value()?

>>> x = pd.DataFrame({'A' : [0,1,2], 'B' : [4,5,6]})
>>> x
   A  B
0  0  4
1  1  5
2  2  6

>>> x.loc[x['A'] == 2]['B']
2    6
Name: B, dtype: int64

>>> type(x.loc[x['A'] == 2]['B'])
<class 'pandas.core.series.Series'>
cs95
  • 379,657
  • 97
  • 704
  • 746
user4979733
  • 3,181
  • 4
  • 26
  • 41
  • 1
    possible duplicate of [How to get a value from a cell of a data frame?](http://stackoverflow.com/questions/16729574/how-to-get-a-value-from-a-cell-of-a-data-frame) – Noah Jun 12 '15 at 22:38
  • I did read the post referenced above. My issue is the conditional indexing part (i.e., x['A'] == 2). Not sure how to get it to work with at(), iat(). Thanks. – user4979733 Jun 12 '15 at 22:42

2 Answers2

70

First of all, you're better off accessing both the row and column indices from the .loc:

x.loc[x['A'] == 2, 'B']

Second, you can always get at the underlying numpy matrix using .values on a series or dataframe:

In : x.loc[x['A'] == 2, 'B'].values[0]
Out: 6

Finally, if you're not interested in the original question's "conditional indexing", there are also specific accessors designed to get a single scalar value from a DataFrame: dataframe.at[index, column] or dataframe.iat[i, j] (these are similar to .loc[] and .iloc[] but designed for quick access to a single value).

Noah
  • 21,451
  • 8
  • 63
  • 71
  • "you're better off accessing both the row and column indices from the .loc" I am wondering what the reason for this is. What do you mean? Why is this better than the way posted in the OP?Thanks – user5359531 Nov 18 '16 at 16:13
  • http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy – Noah Nov 19 '16 at 15:35
  • 21
    Doing `.values[0]` just to get the actual cell value is so clunky. Pandas developers should really improve this. – Jarad Feb 18 '17 at 03:02
  • 4
    `get_value` is deprecated now(v0.21.0 RC1 (October 13, 2017)) [reference is here](https://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#deprecations) `.get_value and .set_value on Series, DataFrame, Panel, SparseSeries, and SparseDataFrame are deprecated in favor of using .iat[] or .at[] accessors (GH15269)` – Shihe Zhang Oct 24 '17 at 03:06
  • 1
    `x.set_index('A').at[2, 'B']` – ihadanny Sep 23 '19 at 11:33
  • Is there any reason not to use min(x['B'][x['A'] == 2]) ? The use of min() seems a but bodgy, but to me the syntax is more logical. – smartse Jan 20 '20 at 16:31
  • The suggested answer uses `.values`. More recent versions of Pandas documentation suggest using `to_numpy` instead. – CheapSquier Sep 17 '20 at 21:22
4

elaborating on @ShineZhang comment:

x.set_index('A').at[2, 'B']

6

pd.__version__

u'0.22.0'

ihadanny
  • 4,377
  • 7
  • 45
  • 76
  • 3
    This answer is exceedingly brief. Perhaps you could provide a little bit of explanation of how it works. – Wyck Sep 23 '19 at 17:05