Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop A specific row In Pandas

Tags:

python

pandas

I tried drop method of pandas but I didn't use it. I mentioned about my specific row in my code block. I caught my row in 'Name' column. How do I remove a specific row in pandas with Python?

e.g.: My specific row is => Name : Bertug Grade: A Age: 15

import pandas as pd , re , string


dataFrame = pd.read_excel("C:\\Users\\Bertug\\Desktop\\example.xlsx")

def vowelCount(s):
   chars = set("aeiouAEIOU")
   for char in s:
       num_vowels = 0
       for ch in char:
           if any((c in chars) for c in ch):
               num_vowels = num_vowels + 1 
           else :
               num_vowels = num_vowels
       if num_vowels == 0:
           print("I have to remove this row in here") 
like image 711
Bertug Avatar asked Mar 31 '17 08:03

Bertug


People also ask

How do I drop a specific row in pandas?

To drop a specific row from the data frame – specify its index value to the Pandas drop function. It can be useful for selection and aggregation to have a more meaningful index. For our sample data, the “name” column would make a good index also, and make it easier to select country rows for deletion from the data.

How do you conditionally drop rows in pandas?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).


1 Answers

df = pd.DataFrame([['Jhon',15,'A'],['Anna',19,'B'],['Paul',25,'D']])
df. columns = ['Name','Age','Grade']

df
Out[472]: 
   Name  Age Grade
0  Jhon   15     A
1  Anna   19     B
2  Paul   25     D

You can get the index of your row:

i = df[((df.Name == 'jhon') &( df.Age == 15) & (df.Grade == 'A'))].index

and then drop it:

df.drop(i)
Out[474]: 
   Name  Age Grade
1  Anna   19     B
2  Paul   25     D

As @jezrael pointed our, you can also just negate all three:

df[((df.Name != 'jhon') &( df.Age != 15) & (df.Grade != 'A'))]
Out[477]: 
   Name  Age Grade
1  Anna   19     B
2  Paul   25     D
like image 80
xgrimau Avatar answered Oct 04 '22 14:10

xgrimau