在 Pandas DataFrame 中删除重复索引的最快方法 [英] Fastest Way to Drop Duplicated Index in a Pandas DataFrame

查看:45
本文介绍了在 Pandas DataFrame 中删除重复索引的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在数据框中删除重复的索引,由于显而易见的原因,以下方法不起作用:

If I want to drop duplicated index in a dataframe the following doesn't work for obvious reasons:

myDF.drop_duplicates(cols=index)

myDF.drop_duplicates(cols='index') 

查找名为索引"的列

如果我想删除一个索引,我必须这样做:

If I want to drop an index I have to do:

myDF['index'] = myDF.index
myDF= myDF.drop_duplicates(cols='index')
myDF.set_index = myDF['index']
myDF= myDF.drop('index', axis =1)

有没有更有效的方法?

推荐答案

很简单:DF.groupby(DF.index).first()

这篇关于在 Pandas DataFrame 中删除重复索引的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆