在 Pandas 中删除特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43136137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drop A specific row In Pandas
提问by Bertug
I tried drop method of pandas but I didnt 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 ?
我尝试了Pandas的 drop 方法,但我没有使用它。我在代码块中提到了我的特定行。我在“名称”列中找到了我的行。如何使用 python 删除Pandas中的特定行?
e.g:-
例如:-
My specific row is => Name : Bertug Grade: A Age: 15
我的具体行是 => 姓名:Bertug 年级:A 年龄: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")
回答by xgrimau
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:
正如@jezrael 指出的那样,您也可以否定所有三个:
df[((df.Name != 'jhon') &( df.Age != 15) & (df.Grade != 'A'))]
Out[477]:
Name Age Grade
1 Anna 19 B
2 Paul 25 D
回答by vaibhav krishna
you can just use :
你可以使用:
df.drop([a,b,c])
where a,b,c
are the list of indexes or row numbers.
a,b,c
索引或行号列表在哪里。
to delete only one particular row use
只删除一个特定的行使用
df.drop(i)
where i
is the index or the row number.
其中i
是索引或行号。