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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
AmirAhmad
  • 153
  • 3
  • 12
  • 1
    Is it similar to the issue referred here http://stackoverflow.com/a/11531402/5916727, you can use `df[df['column_name'].str.contains("your_string")]` – niraj Mar 20 '17 at 17:36

3 Answers3

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)]
Alex
  • 21,273
  • 10
  • 61
  • 73
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
piRSquared
  • 285,575
  • 57
  • 475
  • 624
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
Shenglin Chen
  • 4,504
  • 11
  • 11