Home > Blockchain >  How to remove [None, None] from 2D array
How to remove [None, None] from 2D array

Time:07-25

I am wondering how to remove all [None, None] from this kind of array:

array([['_603686_SS', '_688598_SS'],
       ['_603686_SS', '_688516_SS'],
       ['_603686_SS', '_600563_SS'],
       ...,
       [None, None],
       [None, None],
       [None, None]], dtype=object)

And after removing the [None,None]s, how can I convert the array into a list. Thank you!

CodePudding user response:

use list comprehension with a conditional to remove rows where all values are null

valores = [['_603686_SS', '_688598_SS'],
           ['_603686_SS', '_688516_SS'],
           ['_603686_SS', '_600563_SS'],
           [None, None],
           [None, None],
           [None, None]]
valores = [v for v in valores if v != [None, None]]
valores

or use the 'in' conditional if you want to exclude the rows that at least one value is 'None'

valores = [['_603686_SS', '_688598_SS'],
           ['_603686_SS', '_688516_SS'],
           ['_603686_SS', '_600563_SS'],
           [None, 'Testando'],
           ['Testando', None],
           [None, None]]
valores = [v for v in valores if not(None in v)]
valores

CodePudding user response:

You can use boolean indexing and then convert to a list:

a = np.array([['_603686_SS', '_688598_SS'],
       ['_603686_SS', '_688516_SS'],
       ['_603686_SS', '_600563_SS'],
       [None, None],
       [None, None],
       [None, None]], dtype=object)

list(a[a != None])

Output:

['_603686_SS',
 '_688598_SS',
 '_603686_SS',
 '_688516_SS',
 '_603686_SS',
 '_600563_SS']

Note: this will remove all Nones not all [None, None]s.

  • Related