Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare last value in dataframe

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading