Home > database >  Compare ndarray item with lower case string
Compare ndarray item with lower case string

Time:08-07

I have this block of code:

vals = df.values
col1 = vals[:, 0]
#np.char.lower(col1)
header_start = (col1 == 'name').argmax()
eodf = (col1 == 'pass through').argmax()

col1:

['test sample' 'section' 'Name' 'Mike' 'Nancy' 'Bob' 'Terrance' 'Sara'
 'Myo' 'Pass Through']

I essentially want this to ID the first and last row location and store it into header_start and eodf. I also want to accommodate typos and compare everything in lower case. I can't figure out how to apply lower to the col1 item. Any ideas?

When I try to run that commented line, I get this error: string operation on non-string array

CodePudding user response:

Replace the commented line with this:

for index, i in enumerate(col1):
    col1[index] = i.lower()

It makes each item in the list lowercase.

CodePudding user response:

Create new array with lower case

import numpy as np
col1 = ['test sample', 'section', 'Name', 'Mike', 'Nancy', 'Bob', 'Terrance', 'Sara', 'Myo', 'Pass Through']
new_col1 = np.asarray([i.lower() for i in col1])

header_start = (new_col1 == 'name').argmax()
eodf = (new_col1 == 'pass through').argmax()

print(header_start)
print(eodf)

Or you don't want to create new, just replace col1 array

import numpy as np
col1 = ['test sample', 'section', 'Name', 'Mike', 'Nancy', 'Bob', 'Terrance', 'Sara', 'Myo', 'Pass Through']
col1 = np.asarray([i.lower() for i in col1])

header_start = (col1 == 'name').argmax()
eodf = (col1 == 'pass through').argmax()

print(header_start)
print(eodf)

Result:

2
9
  • Related