Pandas:如何根据列表从数据框中删除行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43269548/
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
Pandas: How to remove rows from a dataframe based on a list?
提问by luisfer
I have a dataframe customerswith some "bad" rows, the key in this dataframe is CustomerID. I know I should drop these rows. I have a list called badcuthat says [23770, 24572, 28773, ...]
each value corresponds to a different "bad" customer.
我有一个带有一些“坏”行的数据框客户,该数据框中的键是 CustomerID。我知道我应该删除这些行。我有一个名为badcu的列表,它表示[23770, 24572, 28773, ...]
每个值对应于不同的“坏”客户。
Then I have another dataframe, lets call it sales, so I want to drop all the records for the bad customers, the ones in the badculist.
然后我有另一个数据框,我们称之为sales,所以我想删除坏客户的所有记录,即badcu列表中的记录。
If I do the following
如果我执行以下操作
sales[sales.CustomerID.isin(badcu)]
I got a dataframe with precisely the records I want to drop, but if I do a
我得到了一个包含我想要删除的记录的数据框,但是如果我执行
sales.drop(sales.CustomerID.isin(badcu))
It returns a dataframe with the first row dropped (which is a legitimate order), and the rest of the rows intact (it doesn't delete the bad ones), I think I know why this happens, but I still don't know how to drop the incorrect customer id rows.
它返回一个数据框,第一行被删除(这是一个合法的顺序),其余的行完好无损(它不会删除坏的),我想我知道为什么会发生这种情况,但我仍然不知道如何删除不正确的客户 ID 行。
回答by Vaishali
You need
你需要
new_df = sales[~sales.CustomerID.isin(badcu)]
回答by piRSquared
You can also use query
你也可以使用 query
sales.query('CustomerID not in @badcu')
回答by Eliethesaiyan
I think the best way is to drop by index,try it and let me know
我认为最好的方法是按索引删除,尝试并告诉我
sales.drop(sales[sales.CustomerId.isin(badcu)].index.tolist())