4

I have one word and a Pandas dataframe with a column of string values. Now I'm trying to find the rows in that dataframe which contain that word in their string part.

I read about extractall() method but I'm not sure how to use it or if its even the right answer.

1
  • 1
    Is it similar to the issue referred here stackoverflow.com/a/11531402/5916727, you can use df[df['column_name'].str.contains("your_string")] Commented Mar 20, 2017 at 17:36

3 Answers 3

4

Using this test data (modified and borrowed from Chris Albon):

raw_data = {'regiment': ['Nighthawks Goons', 'Nighthawks Goons', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
        'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
        'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'],
        'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])

You can use this to find rows that contain the word goons only (I am ignoring the case):

df[df['regiment'].str.contains(r"\bgoons\b", case = False)]
Sign up to request clarification or add additional context in comments.

Comments

2

use str.contains

df.mycolumn.str.contains(myword)

demonstration

myword = 'foo'
df = pd.DataFrame(dict(mycolumn=['abc', '__foo__']))

df.mycolumn.str.contains(myword)

0    False
1     True
Name: mycolumn, dtype: bool

Comments

0

Use jato's example.

In [148]: df[['Goons' in i for i  in  df.regiment]]
Out[148]:
           regiment company      name  preTestScore  postTestScore
0  Nighthawks Goons     1st    Miller             4             25
1  Nighthawks Goons     1st  Jacobson            24             94

Comments

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.