Ask Your Question
1

How to drop 'NAN' values from a list

asked 4 years ago

Cyrille gravatar image

updated 4 years ago

I rewrite my question : I have the followinbg list z=[-1, 0, 1, 2, 3, 'Toto'] and I want to create a list composqe of strictly positive numbers (whichever type they are) and without toto

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 4 years ago

slelievre gravatar image

updated 4 years ago

The question is about filtering a list, keeping only those elements in the list that correspond to positive numbers.

This means filtering out all elements that are not positive numbers.

In particular all strings must be filtered out.

If the list is known to consist only of numbers and 'NAN', or only of numbers and any strings, it is easy to filter out strings.

To exclude values that are equal to the string 'NAN':

 zz = [x for x in z if x != 'NAN' and x > 0]

To exclude all strings:

zz = [x for x in z if not isinstance(x, str) and x > 0]

If list elements can be anything, first check which elements are real numbers, and test positivity for those.

import numbers
zz = [x for x in z if isinstance(x, numbers.Real) and x > 0]
Preview: (hide)
link

Comments

I rewrite my question

Cyrille gravatar imageCyrille ( 4 years ago )

Thanks it's exactly what I was searching but on the way I have had an other idea to obtain what I was searching and I have encountered a new difficulty so I am going to ask a new question. Sorry and 1000000 thanks for your help.

Cyrille gravatar imageCyrille ( 4 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 4 years ago

Seen: 375 times

Last updated: Aug 06 '20