0

I have a pandas DataFrame that contains a year and a title. Some of rows.name are Batman, Catman etc. I am trying to get all rows of the format .*man, using the following expression:

t[t.title & re.search(r'.*man', t.title)]

This Fails. Is there an idiomatic way to accomplish this?

th3an0maly
  • 3,360
  • 8
  • 33
  • 54
forvaidya
  • 3,041
  • 3
  • 26
  • 33

3 Answers3

3
t[t.title & (t.title[-3:] == 'man')]
Julien Goupy
  • 165
  • 6
  • great; this will work ; but I may want put a complex regular expression and I want work around match object is there an operator like ~ or re_match ? – forvaidya Jun 18 '16 at 16:28
  • Use a function : `def select_(t): return t.title[-3:] == 'man'` then t[t.title & select_(t)]. When you will need a more complex selection, just change select_ – Julien Goupy Jun 18 '16 at 16:34
  • means select_t() : must return True or False ? correct . – forvaidya Jun 18 '16 at 16:45
  • I do not know how your DF lib (panda i assume) is working. I just replace my solution in your code. if a bool is expected, yes. – Julien Goupy Jun 18 '16 at 16:48
2

use str.contains:
df[df['title'].str.contains('.man')].

shivsn
  • 7,680
  • 1
  • 26
  • 33
0

following solution worked

df.ix[[x for x in df.index if re.search(r'.*e.*', x)]]

Inspired from || by Pandas - filter and regex search the index of DataFrame

Community
  • 1
  • 1
forvaidya
  • 3,041
  • 3
  • 26
  • 33