python-3.x - 如何在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
谢谢
解决方案
你可以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
推荐阅读
- udeploy - 我正在从 teamcity 触发构建,版本会自动导入 UCD,但没有发生自动部署
- python - 如何在列表右侧添加两个数字
- next.js - 下一个未知的构建错误
- r - 如何按日期索引拆分 XTS 数据帧?
- node.js - Socket.io WebRTC 加载资源失败:服务器响应状态为 404 ()
- xamarin.ios - 导航控制器 Xamarin.ios
- r - 是否可以仅在选定的组内变异?
- assembly - 如何在汇编中打印 Hello World?
- python - 熊猫多重合并创建多维重复列
- javascript - React redux 不会更新类组件的状态