0

I have a DataFrame looking something like this -

enter image description here

Now , how do i extract all the elements in row A having a value greater than 2 ?

In the above case it would be the value 2.706850

I did something like this-

df.loc['A']>2

But i got a series containing Boolean Values something like this -

enter image description here

What should i do to get 2.706850 as the output ?

cs95
  • 379,657
  • 97
  • 704
  • 746

2 Answers2

2

Recommended solution

You can index the dataframe with the conditional expression and the series label itself:

df.loc[df.loc['A'] > 2, 'A']

Old answer, not recommended

Avoid using this approach as it encourages chained assignment. Check the following answer for more details

You just need to index back into the series with your boolean mask as follows:

>>> df.loc['A'][df.loc['A'] > 2]
F    2.706850
Name: A, dtype: float64
cs95
  • 379,657
  • 97
  • 704
  • 746
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • 1
    This isn't a great answer, it encourages chained assignment. You will instead want: `df.loc[df['A'] > 2, 'A']` instead. See https://stackoverflow.com/a/53954986 cc @AdityaKulkarni – cs95 Jan 04 '20 at 05:41
  • 1
    No worries, this is likely a duplicate of something else. Just amend your answer and I'd be happy to upvote. Cheers :-) – cs95 Jan 04 '20 at 05:48
0

Below are tried OK

df.loc['A',df.loc['A']>2]
df.loc['A'][df.loc['A']>2]
MLKu
  • 31
  • 3