'remove all rows including words (character) in numpy 2d array python

How can I remore words in my 2d array: from:

array([['111', 'ACTG1'],
       ['131', '124'],
       ['95', '123'],
       ['95', '124'],
       ['95', 'ACTG1'],
       ['ACTG1', '111'],
       ['ACTG1', '95'],
       ['138', '171']]

to:

array(['131', '124'],
       ['95', '123'],
       ['95', '124'],
       ['138', '171']]

Thanks!



Solution 1:[1]

Try:

arr = arr[np.char.isnumeric(arr).sum(axis=1) == 2]
print(arr)

Prints:

[['131' '124']
 ['95' '123']
 ['95' '124']
 ['138' '171']]

Or:

arr = arr[np.char.isnumeric(arr).all(axis=1)]

Solution 2:[2]

You can use re.compile with numpy.vectorize like below:

import re
pattern = re.compile('[a-zA-Z]')
re_search = np.vectorize(lambda x:bool(pattern.match(x)))
res = arr[~np.any(re_search(arr), axis=1)]
print(res)

Or if it's okay with pandas you can try this:

import pandas as pd
df = pd.DataFrame(arr)
res = df[df.apply(lambda x: x.str.isnumeric()).all(axis=1)].values
print(res)

Output:

[['131' '124']
 ['95' '123']
 ['95' '124']
 ['138' '171']]

Input array:

arr = np.array([['111', 'ACTG1'],['131', '124'],['95', '123'],
                ['95', '124'],['95', 'ACTG1'],['ACTG1', '111'],
                ['ACTG1', '95'],['138', '171']])

Solution 3:[3]

With list comprehension:

a = [['111', 'ACTG1'],
       ['131', '124'],
       ['95', '123'],
       ['95', '124'],
       ['95', 'ACTG1'],
       ['ACTG1', '111'],
       ['ACTG1', '95'],
       ['138', '171']]
[i for i in a if all([j.isnumeric() for j in i[0]+i[1]])]

Output:

[['131', '124'],
  ['95', '123'],
  ['95', '124'],
  ['138', '171']]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Kesely
Solution 2
Solution 3