我想排除那些标题和年份都相同的实例。
title votes ranking year0 Wonderland 19 7.9 19311 Wonderland 120 7.1 19972 Wonderland 3524 7.2 19993 Wonderland 18169 6.6 20034 Wonderland 17 8.7 20105 Wonderland 6 8.5 20126 Wonderland 8 7.4 2012
例如,在这种情况下,我只会删除第5和第6条记录。
回答:
你可以使用带有subset=
参数的drop_duplicates()
。如果你的数据框名为df
,你可以这样做:
In [13]: df.drop_duplicates(subset=['title', 'year'])
这将返回:
Out[13]: title votes ranking year0 Wonderland 19 7.9 19311 Wonderland 120 7.1 19972 Wonderland 3524 7.2 19993 Wonderland 18169 6.6 20034 Wonderland 17 8.7 20105 Wonderland 6 8.5 2012
请注意,你会丢失索引6中包含的votes和ranking的唯一信息。