首页 > 解决方案 > 如何检查另一个数据框中是否不存在字符串值?

问题描述

我有 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_hoursto插入一些值new_df

标签: pythondataframe

解决方案


我不确定你到底想用这两个循环做什么,但你可以使用掩码来过滤你的 df,例如:

mask = ~df_new[col_new].isin(df_hours[col].values)
df[mask]

wherecol_new是某个列,df_new并且col是某个列,df_hours如果需要,您可以查看这些列。


推荐阅读