Ask Your Question
1

How to drop 'NAN' values from a list

asked 2020-08-06 13:33:51 +0100

Cyrille gravatar image

updated 2020-08-06 14:57:07 +0100

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-08-06 14:16:21 +0100

slelievre gravatar image

updated 2020-08-06 16:11:13 +0100

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]
edit flag offensive delete link more

Comments

I rewrite my question

Cyrille gravatar imageCyrille ( 2020-08-06 14:43:48 +0100 )edit

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 ( 2020-08-06 18:53:18 +0100 )edit

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: 2020-08-06 13:33:51 +0100

Seen: 345 times

Last updated: Aug 06 '20