我的文件中有1919行,12列。其中有一列名为Genres,讲述了游戏的类型。示例数据。
Genres
Games, Strategy, Puzzle
Games, Entertainment, Action
...
...
Games, Strategy, Puzzle.
这样一来,就有1919行。我想在不损害原始文档的情况下,选择有谜题的行,并将这些整行存储在一个单独的变量中。就像复制和粘贴一样
我想你有 string
类型 Genres
则答案为
使用 contains
获取词匹配的布尔索引,并最终重置新数据框的索引。
df2 = df[df.Genres.str.contains('Puzzle')].reset_index(drop=True)
df2 = df[df['Genres'] == 'Puzzle'].copy()
这将复制所有带有 Genres == Puzzle
到新的数据框中 df2