5

Consider the data frame

df = pd.DataFrame(numpy.random.randint(0,10,size=(5, 4)), columns=list('ABCD'))
df
   A  B  C  D
0  5  8  0  4
1  7  4  9  0
2  8  1  1  8
3  2  7  6  6
4  4  3  3  0

I would like to filter with loc (the result will be single line) then extract some data out of a certain cell

df.loc[df.A == 7].B
1    4
df.loc[df.A == 7].B.to_string()
'1    4'

The trouble is the index is always getting into the path. How would I get rid of it and/or extract only the cell alone. This example deals with number but I do have columns with other types of data. Any idea ?

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Kenny
  • 1,902
  • 6
  • 32
  • 61

1 Answers1

3

If you just want the values and drop the index, you can for instance:

df.loc[df.A == 7].B.values
#array([4])
Uvar
  • 3,372
  • 12
  • 25
  • but this is showwing array, is there a way to just show 4 ? – Gel Aug 26 '22 at 13:20
  • `df.loc[df.A == 7, 'B'].iloc[0]` <- select the value. I figured the result wasn't necessarily a single integer value. ^^ – Uvar Sep 07 '22 at 16:01