13

I would like to ask an question that is an extension on this thread:

Select rows from a DataFrame based on values in a column in pandas.

The code from this thread is listed below:

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
               'B': 'one one two three two two one three'.split(),
               'C': np.arange(8), 'D': np.arange(8) * 2})
print(df)
#      A      B  C   D
# 0  foo    one  0   0
# 1  bar    one  1   2
# 2  foo    two  2   4
# 3  bar  three  3   6
# 4  foo    two  4   8
# 5  bar    two  5  10
# 6  foo    one  6  12
# 7  foo  three  7  14

print(df.loc[df['D'] == 14])

This will yield the following result:

   A    B      C   D
7  foo  three  7  14

Based on the code above, how can I return a single 'value' not a row. That is, how can I return the value '7' or value 'foo' as opposed to the entire row?

Community
  • 1
  • 1
Nick
  • 555
  • 2
  • 4
  • 13

1 Answers1

19

@JonahWilliams was close, here's a working one:

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
               'B': 'one one two three two two one three'.split(),
               'C': np.arange(8), 'D': np.arange(8) * 2})

print(df.loc[df['D'] == 14]['A'].index.values)

>>>[7]

print(df.loc[df['D'] == 14]['A'].values)

>>>['foo']
Leb
  • 15,483
  • 10
  • 56
  • 75
  • Fantastic. Thanks very much to both of you. – Nick Oct 09 '15 at 00:22
  • These return ndarrays of length 1, rather than the actual values. My solution is min(df['A'][df['D'] == 14]) and this syntax is more logical to me, although using min() seems like a bodge. Surely there is a better way though? – smartse Jan 20 '20 at 16:17
  • 2
    @smartse - Rather than using `min()`, I'll typically use indexing (`[0]`) to get the **first** value, like this: `df[df['D'] == 14].loc[:, 'A'].values[0]`. Albeit, I'll only be expecting **one value** to be returned. – S3DEV Mar 04 '20 at 16:20