0

Is it possible to use conditional if with a value extracted of a DataFrame with iloc?
Something like this:

if dta.iloc[[1],[5]] == "Example":
    print("Ok")
taras
  • 6,566
  • 10
  • 39
  • 50
  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Aug 21 '18 at 08:12
  • But it seems need `if (dta.iloc[[1],[5]]=="Example").any():` – jezrael Aug 21 '18 at 08:13

2 Answers2

0

I think you are close, need remove nested [] for scalar:

dta.iloc[1,5]

Sample:

dta = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (dta)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

print (dta.iloc[1,5])
a

if dta.iloc[1,5]=="a":
    print("Ok")

Ok

But if iloc return Series add Series.any for check if exist at least one True:

print (dta.iloc[1:4,5])
1    a
2    a
3    b
Name: F, dtype: object

print (dta.iloc[1:4,5] == 'a')
1     True
2     True
3    False
Name: F, dtype: bool

print ((dta.iloc[1:4,5] == 'a').any())
True

And for DataFrame simpliest is use numpy.any for check at least one True in 2d array:

print (dta.iloc[1:4,4:6])
   E  F
1  3  a
2  6  a
3  9  b

print (dta.iloc[1:4,4:6] == 'a')
       E      F
1  False   True
2  False   True
3  False  False

print ((dta.iloc[1:4,4:6] == 'a').values.any())
True
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @GerardBoxóCorominas, There's no need to apologize for the question. BUT what you *should* do is go back and [edit your question](https://stackoverflow.com/posts/51944340/edit) so that it is more precise/useful for the wider community, e.g. with a [mcve]. – jpp Aug 21 '18 at 08:20
0

I think u can use values.

In your example:

if dta.iloc[[1],[5]].values=="Example": print("Ok")