Python & Pandas: How to query if a list-type column contains something?

前端 未结 5 861
攒了一身酷
攒了一身酷 2020-11-27 17:59

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:

         


        
5条回答
  •  孤独总比滥情好
    2020-11-27 18:45

    using sets

    df.genre.map(set(['comedy']).issubset)
    
    0     True
    1     True
    2    False
    3    False
    dtype: bool
    

    df.genre[df.genre.map(set(['comedy']).issubset)]
    
    0             [comedy, sci-fi]
    1    [action, romance, comedy]
    dtype: object
    

    presented in a way I like better

    comedy = set(['comedy'])
    iscomedy = comedy.issubset
    df[df.genre.map(iscomedy)]
    

    more efficient

    comedy = set(['comedy'])
    iscomedy = comedy.issubset
    df[[iscomedy(l) for l in df.genre.values.tolist()]]
    

    using str in two passes
    slow! and not perfectly accurate!

    df[df.genre.str.join(' ').str.contains('comedy')]
    

提交回复
热议问题