如何在 pandas 中按列表删除行 [英] How to drop rows by list in pandas

查看:55
本文介绍了如何在 pandas 中按列表删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有了数据框和列表.

Now I have dataframe and list.

A B
1 a 
2 b
3 c
4 d
5 e

list = [a,b,c]

我想通过df.B引用列表删除行.

I would like to drop rows by df.B refering to list.

我想在df以下

A B
4 d
5 e

如何获得此结果?

推荐答案

您可以使用

You can use isin with inverted mask by ~.

我认为 list python 中不是一个好名字,最好是 L ,因为 list 是代码字如果分配变量,您可以覆盖它:

I think list is not good name in python, better is L, because list is code word and if assign variable you override it:

L= ['a','b','c']

print (df[~df.B.isin(L)])
   A  B
3  4  d
4  5  e

这篇关于如何在 pandas 中按列表删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆