0

I have a numpy array with 4 columns. The first column is text.

I want to retrieve every row in the array where the first column contains a substring.

Example: if the string I'm searching for is "table", find and return all rows in the numpy array whose first column contains "table."

I've tried the following:

rows = nparray[searchString in nparray[:,0]]

but that doesn't seem to work

3
  • Is pandas available? Because if it is this is easy. Commented Jul 6, 2018 at 16:23
  • The array I'm using is literally a pandas dataframe derived from as_matrix(), so it's very much available. What did I miss? Commented Jul 6, 2018 at 16:24
  • 2
    This: stackoverflow.com/questions/17071871/…. Commented Jul 6, 2018 at 16:27

1 Answer 1

2

Given a pandas DataFrame df, this will return all rows where searchString is a substring of the value in the column column:

searchString = "table"

df.loc[df['column'].str.contains(searchString, regex=False)]
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, after Paula Thomas led me down the pandas track, I came across this stackoverflow.com/questions/27975069/…, which is the same thing. Thanks!
Add regex=False for a trivial speed-up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.