-1

For instance, I have reviews column and I want to extract words and create dummy variables based on them.

I use that but can't use regular expressions here:

df = df['reviews'].str.contains('good').astype(int)

How can I use regular expressions here for extracting good, goid, goof etc.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Said Taxmezov
  • 67
  • 1
  • 7

1 Answers1

0

You can use the .map method after the .contains method. Here's a concise example:

import pandas as pd

df = pd.DataFrame({'id': [1,2,3],
                  'review': ['this is a good one',
                            'this is a bad one',
                            'this one seems to be fine']})

df['review_good'] = df.review.str.contains('good').map({True: 1, False: 0})