1

I need to select the rows that are according to the following line:

Like this is for numbers:

data[data['field']<4]

I need this: where the words do are inside the name of a given row

data[data['field'] LIKE '%do%']

and this: where it starts with 01

data[data['field'] LIKE '01%']

I think it needs regex but I don't know how to do it

2

You can use simple patterns or more advanced regex patterns in the str.contains() method:

data[data['field'].str.contains('do')]

For the case where the strings needs to start with a pattern, you can either use a regex pattern in contains (^ denotes the pattern should be at the beginning) or match which defaults to looking for patterns starting from the beginning:

data[data['field'].str.contains('^01')]
data[data['field'].str.match('01')]
  • Can you tell me also how to do the opposite, to leave out those that start with 01? – user122244 Jun 12 '18 at 14:19
  • You can "invert" a mask selection using~: data[~data['field'].contains(..)] – joris Jun 12 '18 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.