简体   繁体   English

熊猫:删除具有唯一索引值的数据框行

[英]Pandas: remove rows of dataframe with unique index value

I am trying to remove the rows of my dataframe (df) which have unique value as index. 我正在尝试删除数据框(df)中具有唯一值作为索引的行。 This is my df: 这是我的df:

    A       B
1   3.803   4.797
1   3.276   3.878
2   5.181   6.342
3   6.948   9.186
3   8.762  10.136
4  10.672  12.257
4   8.266  13.252
5  13.032  14.656
6  15.021  17.681
6  16.426  15.07

I would like to remove the rows with index=2,5 to get a new dataframe (df_new) as follow: 我想删除索引= 2,5的行,以获取一个新的数据框(df_new),如下所示:

    A       B
1   3.803   4.797
1   3.276   3.878
3   6.948   9.186
3   8.762  10.136
4  10.672  12.257
4   8.266  13.252
6  15.021  17.681
6  16.426  15.07

Is there some handy function in pandas to do that? 熊猫有一些方便的功能吗? Thank you 谢谢

Use get_duplicates : 使用get_duplicates

In [36]:
df.loc[df.index.get_duplicates()]

Out[36]:
        A       B
1   3.803   4.797
1   3.276   3.878
3   6.948   9.186
3   8.762  10.136
4  10.672  12.257
4   8.266  13.252
6  15.021  17.681
6  16.426  15.070

get_duplicates returns an array of the duplicated indices: get_duplicates返回重复索引的数组:

In [37]:
df.index.get_duplicates()

Out[37]:
[1, 3, 4, 6]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM