用户登录
用户注册

分享至

在 Pandas DataFrame 中删除重复索引的最快方法

  • 作者: 小枫兜兜
  • 来源: 51数据库
  • 2022-10-27

问题描述

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

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()

软件
前端设计
程序设计
Java相关