首页 > 解决方案 > 如何在python中删除重复项?

问题描述

我有一个如下的数据框:

print(df)

   Product  Color   Weight  
0     A      Red     13.01
1     A      Red     13.04
2     A      Red     13.10
3     A      Red     13.11

我想删除重复项并仅保存重量为 max() 的产品。

print(df)

   Product  Color   Weight  
0     A      Red     13.11

谢谢

标签: python-3.xpandasduplicates

解决方案


你可以groupby使用.max

#if you don't care about color remove it from the groupby clause.
#df.groupby(['Product'])['Weight'].max().reset_index()
df1 = df.groupby(['Product','Color'])['Weight'].max().reset_index()

print(df1)

  Product Color  Weight
0       A   Red   13.11

推荐阅读