gpt4 book ai didi

python - 考虑 Pandas 中的几个属性,删除重复项

转载 作者:行者123 更新时间:2023-11-30 09:55:31 30 4
gpt4 key购买 nike

我想排除那些具有相同标题和同一年份的实例。

     title      votes  ranking  year
0 Wonderland 19 7.9 1931
1 Wonderland 120 7.1 1997
2 Wonderland 3524 7.2 1999
3 Wonderland 18169 6.6 2003
4 Wonderland 17 8.7 2010
5 Wonderland 6 8.5 2012
6 Wonderland 8 7.4 2012

例如,在本例中。我只会删除 5 或 6

最佳答案

您可以使用drop_duplicates()subset= 参数。如果您的数据框名为 df,您需要执行以下操作:

In [13]: df.drop_duplicates(subset=['title', 'year'])

将返回:

Out[13]:
title votes ranking year
0 Wonderland 19 7.9 1931
1 Wonderland 120 7.1 1997
2 Wonderland 3524 7.2 1999
3 Wonderland 18169 6.6 2003
4 Wonderland 17 8.7 2010
5 Wonderland 6 8.5 2012

请注意,您会丢失索引 6 中包含的投票和排名中的任何独特信息。

关于python - 考虑 Pandas 中的几个属性,删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32342692/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com