1

Dataset: fandango_score_comparison.csv

I'm trying to access a row that matches a given movie name using the following code:

df = pd.read_csv("http://github.com/mircealex/Movie_ratings_2016_17/raw/master/fandango_score_comparison.csv")

df.drop_duplicates(subset=["FILM"], inplace=True, ignore_index=True)

movie_name = df.FILM.iloc[0]
movie_df = df[df["FILM"].str.contains(movie_name)]

But the movie_df I get is always empty, irrespective of the movie_name I select. What am I missing or doing wrongly?

3
  • 3
    Try df[df["FILM"].str.contains(movie_name), regex=False]. contains assumes the argument is a regular expression. Your first movie name may accidentally be a valid regex. Commented Dec 14, 2022 at 23:21
  • 2
    You probably meant df[df["FILM"].str.contains(movie_name, regex=False)]. Commented Dec 14, 2022 at 23:23
  • Thanks, it's resolved with 'regex' parameter in contains(). Commented Dec 14, 2022 at 23:31

1 Answer 1

1

As noted in the comments, and as documented here, pandas.Series.str.contains() takes a regex= parameter, which by default is True. This means that if your movie_name contains special regular-expression characters (such as *, (), [], and so on), it will be interpreted as a regular expression, which is most likely what is happening.

You should be ok if you explicitly disable regular expressions:

movie_df = df[df["FILM"].str.contains(movie_name, regex=False)]
Sign up to request clarification or add additional context in comments.

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.