DataFrame.drop 不会删除 Pandas 中的预期行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19741997/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:18:01  来源:igfitidea点击:

DataFrame.drop not dropping expected rows in Pandas

pythonpandas

提问by Chris Fonnesbeck

I have a Pandas DataFrame that includes rows that I want to drop based on values in a column "population":

我有一个 Pandas DataFrame,其中包含我想根据“population”列中的值删除的行:

data['population'].value_counts()

general population                          21
developmental delay                         20
sibling                                      2
general population + developmental delay     1
dtype: int64

here, I want to drop the two rows that have siblingas the value. So, I believe the following should do the trick:

在这里,我想删除具有sibling作为值的两行。所以,我相信以下应该可以解决问题:

data = data.drop(data.population=='sibling', axis=0)

It does drop 2 rows, as you can see in the resulting value counts, but they were notthe rows with the specified value.

正如您在结果值计数中看到的那样,它确实删除了 2 行,但它们不是具有指定值的行。

data.population.value_counts()

developmental delay                         20
general population                          19
sibling                                      2
general population + developmental delay     1
dtype: int64

Any idea what is going on here?

知道这里发生了什么吗?

回答by joaquin

dataFrame.dropaccepts an index (list of labels) as a parameter, not a mask.
To use dropyou should do:

dataFrame.drop接受索引(标签列表)作为参数,而不是掩码。
要使用drop你应该这样做:

data = data.drop(data.index[data.population == 'sibling'])

however it is much simpler to do

但是做起来要简单得多

data = data[data.population != 'sibling']