11

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
  • 14,495
  • 9
  • 52
  • 73
Ashwin Jayarama
  • 231
  • 1
  • 5
  • 13

3 Answers3

30

You can also just slice your df using iloc:

df.iloc[::2]

This will step every 2 rows

EdChum
  • 339,461
  • 188
  • 752
  • 538
5

You can try this way :

df = df[df.type != "Low"]
AntonyBrd
  • 383
  • 1
  • 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
  • 14,495
  • 9
  • 52
  • 73