在Pandas中根据多个属性去除重复项

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

     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的唯一信息。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注