python - 如何检查另一个数据框中是否不存在字符串值?
问题描述
我有 2 个数据框需要关注:df_hours 和 new_df
我想检查整个数据帧(new_df)中是否不存在数据帧(df_hours)的字符串值。
例如,df_hours 有一个“类别”列,其中包含字符串值“A”、“B”、“C”等。我想检查 new_df 中是否不存在“A”。
我有 2 个 for 循环,在其中我有以下 if 条件:
for i in range(len(df_hours)):
for j in range(len(df_hours_copy)):
if df_hours.iloc[i,1] == df_hours_copy.iloc[j,1] and (~df_hours.iloc[i,1].isin(new_df)):
如何编码 if 的第二部分('and' 之后的那个)?
想法:通过之后的代码and
,我只想检查 new_df 中是否不存在该值,然后从df_hours
to插入一些值new_df
。
解决方案
我不确定你到底想用这两个循环做什么,但你可以使用掩码来过滤你的 df,例如:
mask = ~df_new[col_new].isin(df_hours[col].values)
df[mask]
wherecol_new
是某个列,df_new
并且col
是某个列,df_hours
如果需要,您可以查看这些列。
推荐阅读
- typescript - 如何在 TypeScript 中初始化具有额外属性的数组对象?
- ios - DispatchSemaphore 停止所有功能
- bash - 在 grep (BASH) 中需要帮助
- javascript - JAMStack - 小型电子商务商店
- python - Openpyxl - 寻找获得此列表的最佳选择,对字典进行排序
- vim - listchars 导致 E474:最近版本的 vim 中的参数无效
- c++ - C++ 中重载乘法运算符的递归类模板实例化期间出错
- javascript - 使用 fetch 或 XMLHttpRequest 将此 curl POST 转换为 javascript POST 会出现错误
- ios - 如何使用 IndexSet 在自定义结构中查找项目
- gatsby - 修改 Gatsby Contentful 博客文章的 URL