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")
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")
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
I think u can use values.
In your example:
if dta.iloc[[1],[5]].values=="Example":
print("Ok")