12

I have a pandas dataframe with duplicate ids. Below is my dataframe

id  nbr  type  count 
7   21   High     4  
7   21   Low      6    
8   39   High     2    
8   39   Low      3    
9   13   High     5    
9   13   Low      7    

How to delete only the rows having the type Low

Leb
  • 15,483
  • 10
  • 56
  • 75
Ashwin Jayarama
  • 261
  • 1
  • 6
  • 14

3 Answers3

33

You can also just slice your df using iloc:

df.iloc[::2]

This will step every 2 rows

EdChum
  • 376,765
  • 198
  • 813
  • 562
5

You can try this way :

df = df[df.type != "Low"]
AntonyBrd
  • 403
  • 2
  • 10
3

Another possible solution is to use drop_duplicates

df = df.drop_duplicates('nbr')
print(df)

   id  nbr  type  count
0   7   21  High      4
2   8   39  High      2
4   9   13  High      5

You can also do:

df.drop_duplicates('nbr', inplace=True)

That way you don't have to reassign it.

Leb
  • 15,483
  • 10
  • 56
  • 75