【发布时间】: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 的答案似乎更好