I am trying to remove the rows of my dataframe (df) which have unique value as index. This is my 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:
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
:
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:
In [37]:
df.index.get_duplicates()
Out[37]:
[1, 3, 4, 6]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.