Compare last value in dataframe

Advertisements

I have this dataframe:

distance

0       0.000000
1       0.001956
2       0.001601
3       0.001601
4       0.003379
      ...   
1236    0.150453
1237    0.152232
1238    0.157923
1239    0.159701
1240    0.159879

Name: close, Length: 1241, dtype: float64

I want to check if last value in dataframe is > 15 or not.

I tried it the following way:

if distance.tail(1) > 15:
   print('bigger > 15')
else:
   print('less < 15')

which gives me the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), 
a.any() or a.all().

How can I get around this?

>Solution :

df.tail returns a Series.

Use df.iloc:

if distance.iloc[-1] > 15:
   print('> 15')
else:
   print('<= 15')

Leave a ReplyCancel reply