将行添加到另一个变量中。这些行已经在另一个csv文件中可用。

问题描述 投票:0回答:1

我的文件中有1919行,12列。其中有一列名为Genres,讲述了游戏的类型。示例数据。

Genres

Games, Strategy, Puzzle 

Games, Entertainment, Action 

...

... 

Games, Strategy, Puzzle.

这样一来,就有1919行。我想在不损害原始文档的情况下,选择有谜题的行,并将这些整行存储在一个单独的变量中。就像复制和粘贴一样

python pandas csv syntax jupyter-notebook
1个回答
1
投票

我想你有 string 类型 Genres 则答案为

使用 contains 获取词匹配的布尔索引,并最终重置新数据框的索引。

df2 = df[df.Genres.str.contains('Puzzle')].reset_index(drop=True)

0
投票

df2 = df[df['Genres'] == 'Puzzle'].copy()这将复制所有带有 Genres == Puzzle 到新的数据框中 df2

© www.soinside.com 2019 - 2024. All rights reserved.