Search code examples
pythonpandasextractpython-re

How to get the rows which contain a specific words?


I'm using netfilx dataset. uploaded by VICTOR SOEIRO on KAGGLE. I want to make new datasets based on genres . For example, I want new dataframe if genres contain word ['comedy'] or ['comedy','fantasy'].

My_code
df_comedy = df[df['genres'].str.extract("'comedy'")]

It gives me this error. ValueError: pattern contains no capture groups

Ask me to provide more information, If you don't understand my question.

Thank you.


Solution

  • Here is Solution for your problem:

    import pandas as pd
    #reading csv file
    df = pd.read_csv("Assignment.csv")
    
    # filtering the rows where Credit-Rating is Fair
    df = df[df['Credit-Rating'].str.contains('Fair')]
    print(df)
    

    output:

    job        Age_Range    Salary    Credit-Rating  Savings    
    
    Own        Middle-aged  High       Fair          10000    
    Govt       Young        Low        Fair          15000   
    Private    Senior       Average    Fair          20000  
    Own        Middle-aged  High       Fair          13000   
    Own        oung         Low        Fair          17000  
    

    Use contains instead of extract, you will get your answer.