使用层次结构条件SQL Python Pandas删除重复项
我需要在大型数据库中删除重复项,但是要删除的行必须基于使用sqlite或python pandas的层次结构。是否有一种有效的方法可以解决此问题?最好使用Python Pandas DataFrame,但SQLite也可以。
ID | 文本 | 类别 |
---|---|---|
1 | 文本 | 优先级3 |
2 | 文本 | 优先级1 |
3 | 文本 | 优先2 |
4 | 文本2 | 优先级3 |
5 | 文本2 | 优先级 |
应转换为:
ID | 文本 | 类别 |
---|---|---|
2 | 文本 | 优先级1 |
5 | 文本2 | 优先级2优先级2 |
I need to delete duplicates in a large database, but the rows to be deleted must be based on a hierarchy using either SQLite or Python Pandas. Is there a efficient way to relize this? preferably using python pandas dataframe but SQLite is also fine.
ID | Text | Category |
---|---|---|
1 | text | Priority 3 |
2 | text | Priority 1 |
3 | text | Priority 2 |
4 | text 2 | Priority 3 |
5 | text 2 | Priority 2 |
should turn to this:
ID | Text | Category |
---|---|---|
2 | text | Priority 1 |
5 | text 2 | Priority 2 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下尝试:
输出:
Try this:
Output:
@drakax非常相似的方法,但使用
drop_duplicates
而不是groupby
和首先
Very similar approach to @Drakax but using
drop_duplicates
instead ofgroupby
andfirst
避免在可能的时候进行分类,请使用
分类
来定义优先级的顺序,并获取每组最小的索引:输出:
Avoid sorting when you can, use a
Categorical
to define the order of the priorities and get the index of the min per group:Output: