【问题标题】:How to find the unique list entries in a python dataframe?如何在 python 数据框中找到唯一的列表条目?
【发布时间】:2019-05-28 04:50:18
【问题描述】:

我有一个数据集,其中包含电影名称以及它所属的不同类型。每部电影都有不止一种类型。因此,对于整个数据集,我想找到存在的独特类型的总数。

我无法使用df.unique(),因为它是 DataFrame 本身每一列中的一个列表。

movieId title   genres
0   1   Toy Story (1995)    Adventure|Animation|Children|Comedy|Fantasy
1   2   Jumanji (1995)  Adventure|Children|Fantasy
2   3   Grumpier Old Men (1995) Comedy|Romance
3   4   Waiting to Exhale (1995)    Comedy|Drama|Romance
4   5   Father of the Bride Part II (1995)  Comedy
5   6   Heat (1995) Action|Crime|Thriller
6   7   Sabrina (1995)  Comedy|Romance
7   8   Tom and Huck (1995) Adventure|Children
8   9   Sudden Death (1995) Action
9   10  GoldenEye (1995)    Action|Adventure|Thriller
10  11  American President, The (1995)  Comedy|Drama|Romance
11  12  Dracula: Dead and Loving It (1995)  Comedy|Horror
12  13  Balto (1995)    Adventure|Animation|Children
13  14  Nixon (1995)    Drama
14  15  Cutthroat Island (1995) Action|Adventure|Romance
15  16  Casino (1995)   Crime|Drama
16  17  Sense and Sensibility (1995)    Drama|Romance
17  18  Four Rooms (1995)   Comedy
18  19  Ace Ventura: When Nature Calls (1995)   Comedy
19  20  Money Train (1995)  Action|Comedy|Crime|Drama|Thriller
20  21  Get Shorty (1995)   Comedy|Crime|Thriller
21  22  Copycat (1995)  Crime|Drama|Horror|Mystery|Thriller
22  23  Assassins (1995)    Action|Crime|Thriller
23  24  Powder (1995)   Drama|Sci-Fi
24  25  Leaving Las Vegas (1995)    Drama|Romance
25  26  Othello (1995)  Drama
26  27  Now and Then (1995) Children|Drama
27  28  Persuasion (1995)   Drama|Romance
28  29  City of Lost Children, The (Cité des enfants p...   

这是电影的数据集。

在类型列下,我想将Action|Comedy|Crime|Drama|Thriller 拆分为动作、喜剧、犯罪、戏剧、惊悚片。

对于现在作为 DataFrame 的整个数据集,我想找到独特的流派。

【问题讨论】:

  • 您是否尝试过先将所有流派列收集到一个数组中,然后调用 .unique()?
  • 不,还没有。我对python很陌生,因此我对此并不熟悉。我会试试的。
  • 我试过了,它确实有效。但它只是需要时间来运行。谢谢!
  • 很高兴它成功了!无论如何,AkshayNevrekar 的答案似乎更好

标签: python pandas


【解决方案1】:

尝试使用这种方法:

temp = df.genres.str.split("|").tolist() # this will return a list of lists for all the genres
import functools
import operator

unique_genres = set(functools.reduce(operator.concat, temp)) #this will flatten the list of lists and ultimately call the set to get the unique genres. Use len to get the number of unique genres afterwards

【讨论】:

    【解决方案2】:

    你可以这样做:

    df = pd.DataFrame({'title':['Toy Story (1995)','Jumanji (1995)','Grumpier Old Men (1995)'],
                                'genres':['Adventure|Animation|Children|Comedy|Fantasy','Adventure|Children|Fantasy','Comedy|Romance']})
    
    
    a = list(set([y for x in df['genres'] for y in x.split('|')]))
    print(a)
    

    输出:

    ['Animation', 'Comedy', 'Children', 'Fantasy', 'Adventure', 'Romance']
    

    【讨论】:

    • 这也很好用。但是 ashish14 给出的那个似乎更快。无论如何,谢谢!
    【解决方案3】:

    尝试以下操作:

    df = pda.read_csv('movies.csv')
    df['genres'] = df['genres'].apply(lambda x: x.strip().split('|'))
    df['count'] = df['genres'].apply(lambda y: len(y))
    print(df)
    
    OUTPUT :
    
       movie   Id  ...                                             genres count
         0    1  ...  [Adventure, Animation, Children, Comedy, Fantasy]     5
         1    2  ...                     [Adventure, Children, Fantasy]     3
         2    3  ...                                  [Comedy, Romance]     2
         3    4  ...                           [Comedy, Drama, Romance]     3
         4    5  ...                                           [Comedy]     1
         5    6  ...                          [Action, Crime, Thriller]     3
    

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多