[Python] How to query if a list-type column contains something?

Discussão em 'Python' iniciado por Stack, Novembro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe, which contains info about movies. It has a column called genre, which contains a list of genres it belongs to. For example:

    df['genre']

    ## returns

    0 ['comedy', 'sci-fi']
    1 ['action', 'romance', 'comedy']
    2 ['documentary']
    3 ['crime','horror']
    ...


    I want to know how can I query the dataframe, so it returns the movie belonging to a certain genre?

    For example, something like df['genre'].contains('comedy') returns 0 or 1.

    I know for a list, I can do things like:

    'comedy' in ['comedy', 'sci-fi']


    However, in pandas, I didn't find something similar, the only thing I know is df['genre'].str.contains(), but it didn't work for the list type.

    Continue reading...

Compartilhe esta Página