python - 如何比较数据框中的确切字符串?
问题描述
我正在尝试使用数据框修改用 Python 编写的前同事的代码。
我们想从数据框中删除所有df_msc
不包含drives_tup
“已安装”列中的字符串之一的行。他使用字符串修饰符startswith
and对其进行了修剪endswith
,但这仍然会通过不需要的行。有没有办法做类似的事情string.isequalto(drives_tup)
?
我不擅长 Python,所以我不想过多地改变格式,因为我担心它会破坏代码的其他部分......
# Take only those drives where they start or end with one of the drives_tup values
drives_tup = ("/usr/asm", "/data/reserved", "/var", "/", "/data/diagnostics")
df_msc = df_msc.iloc[:, 1:-1].dropna(how='all')
df_msc = df_msc.loc[df_msc['Mounted'].str.startswith(drives_tup)]
df_msc = df_msc.loc[df_msc['Mounted'].str.endswith(drives_tup)]
解决方案
df_msc = df_msc.loc[df_msc['Mounted'].isin(drives_tup)]
推荐阅读
- python - 在 GPFlow 中使用自定义内核进行分类时出现 Cholesky 分解问题
- python - 使用 QlistWidget 进行主动搜索
- c# - 哪个 nuget 包包含方法 GetSheetService()?
- php - 根据 WooCommerce Checkout 中的选择字段添加动态费用
- yaml - Swagger 文档保存在 API 管理前端时缺少架构
- javascript - e.stopPropagation() 不适用于标签内嵌套元素的 onClick 事件
- javascript - 为什么 Mapbox Place Search(前向地理编码)坐标与 v11 地图上的要素不匹配?
- django - Django 管理员详细信息页面链接到外部 URL
- javascript - 反应钩子帮助重构基于类的组件
- spring-data-mongodb - Spring Data Mongo 检查是否有数据或记录