查找不在两个数据框中的行

时间:2021-06-03 19:17:38

I have the following dataframe:

我有以下数据帧:

symbol,    name
abc        Jumping Jack
xyz        Singing Sue
rth        Fat Frog

I then have another dataframe with the same structure (symbol + name). I need to output all the symbols which are in the first dataframe but not the second.

然后我有另一个具有相同结构的数据帧(符号+名称)。我需要输出第一个数据帧中的所有符号,但不输出第二个数据帧中的所有符号。

The name column is allowed to differ. For example I could have symbol = xyz in both dataframes but with different names. That is fine. I am simply trying to get the symbols which do not appear in both dataframes.

名称列允许不同。例如,我可以在两个数据帧中使用symbol = xyz但名称不同。那样就好。我只是想获得两个数据帧中都没有出现的符号。

I am sure this can be done using pandas merge and then outputting the rows that didn't merge, but I just can't seem to get it right.

我确信这可以使用pandas merge完成,然后输出没有合并的行,但我似乎无法正确使用它。

1 个解决方案

#1


2  

Use isin and negate the condition using ~:

使用isin并使用〜来否定条件:

df[~df['symbol'].isin(df1['symbol'])]

This will return rows where 'symbol' is present in your first df and not in the other df

这将返回第一个df中存在“symbol”但不存在于另一个df中的行

#1


2  

Use isin and negate the condition using ~:

使用isin并使用〜来否定条件:

df[~df['symbol'].isin(df1['symbol'])]

This will return rows where 'symbol' is present in your first df and not in the other df

这将返回第一个df中存在“symbol”但不存在于另一个df中的行