python - 从 Pandas DF 中删除包含列表中元素的行
问题描述
假设我有一个 DF:
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
('Riti', 30, 'Delhi' , 'India' ) ,
('Vikas', 31, 'Mumbai' , 'India' ) ,
('Neelu', 32, 'Bangalore' , 'India' ) ,
('John', 16, 'New York' , 'US') ,
('Mike', 17, 'las vegas' , 'US') ]
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])
我有一个清单:
[Vikas, Neelu, Jack]
如何从我的 DF 中删除包含此列表中元素的行。我的谷歌搜索只向我展示了如何按列索引或条件(如生命值)来删除列低于或高于某个整数值
解决方案
remove_words = ['Vikas', 'Neelu', 'Jack']
result = dfObj[~dfObj.Name.isin(remove_words)]
# display(result)
Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi India
e John 16 New York US
f Mike 17 las vegas US
忽略大小写
- 注意
'Jack'
不一样'jack'
map
remove_words
小写 (str.lower
)- 进行布尔检查时
转换
Name
为小写。pandas.Series.str.lower
- 这将使
Name
列中值的大小写保持不变。
- 这将使
# map the list of words to lowercase
remove_words = list(map(str.lower, ['Vikas', 'Neelu', 'Jack']))
# cast the Name column as lowercase when checking remove_words
result = dfObj[~dfObj.Name.str.lower().isin(remove_words)]
# display(result)
Name Age City Country
b Riti 30 Delhi India
e John 16 New York US
f Mike 17 las vegas US
推荐阅读
- javascript - 了解异步返回函数(未定义)
- android - 我将如何为我的 ClusterManager 中的每个标记启动自定义 URI 意图?
- solr - Solr 突出显示 synonyms.txt 中的单词以及查询词
- r - 我无法弄清楚如何使用双变量正态分布创建多步代码
- java - Apache Camel:无法删除文件并锁定
- javascript - 如何使用 serializeArray 获取 post 表单的数据?
- pyspark - Pyspark 中的多个过滤器
- powershell - Powershell Start-Process cmdlet 在 TFS Powershell 脚本中不起作用
- expo - 世博会:不在 AppLoading 上加载资产
- s4sdk - 尝试使用 S/4HANA Cloud SDK 使用来自 S/4HANA Cloud 的数据源创建自定义 OData v2 服务