Home > Net >  remove all rows including words (character) in numpy 2d array python
remove all rows including words (character) in numpy 2d array python

Time:04-16

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!

CodePudding user response:

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)]

CodePudding user response:

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']])

CodePudding user response:

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']]
  • Related