0

I have a data similar to,

import numpy as np
A = np.array( [['1','2','3'], ['a','3','5']] )

Now I want to identify the cell address of 'a'. I have tried the following code for that purpose,

for i in range(0,2):
    for j in range(0,3):
        if (type(float(A[i,j])) == float):
            print(str(i)+str(j))

since, 'a' can't be converted into floating point it shows the following error.

00

01

02

Traceback (most recent call last):

File "", line 3, in

if (type(float(A[i,j])) == float):

ValueError: could not convert string to float: 'a'

Please help. Thank you in advance.

10
  • Don't you need to use A[i][j] or is that some fancy numpy magic? Commented Nov 20, 2017 at 18:41
  • Your data is going to be just one character? Then you could use isdigit and so docs.python.org/3/library/stdtypes.html Commented Nov 20, 2017 at 18:41
  • @SumnerEvans the __getitem__ of numpy arrays accepts tuples! Commented Nov 20, 2017 at 18:45
  • 2
    everything is a string in your array. Why are you even using numpy here? Commented Nov 20, 2017 at 18:45
  • 1
    @timgeb, that's cool! I've never used numpy before, so that's really neat. Commented Nov 20, 2017 at 18:46

1 Answer 1

1

You can try this

import numpy as np

def is_number(s):
   try:
    int(s)
    return True
   except ValueError:
    return False

A = np.array( [['1','2','3'], ['a','3','5']] )

for i in range(0,2):
    for j in range(0,3):
        if not is_number(A[i][j]):
            print i , j
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.