I have two dataframes:
df1 = pd.DataFrame({'System':['b0001','b0002']})
df2 = pd.DataFrame({'System':['b0001']})
I want to print the value in column System of df1 that is NOT contained in column System of df2. The output should only be:
b0002
My current code is:
for i in df1.index:
if df1.System[i] not in df2.System:
print (df1.System[i])
But the output is:
b0001
b0002
I cant'f figure out why it still prints out b0001
. I've tried with isin
and the output is the same.
Any help will be appreciated.
A pandas way of doing this is to use isin
as follows:
df1[~df1.System.isin(df2.System)]
Output:
System
1 b0002
However, to do it the way you are doing you are missing .values
:
for i in df1.index:
if df1.System[i] not in df2.System.values:
print (df1.System[i])
Output:
b0002
numpy
np.setdiff1d(df1.System.values, df2.System.values)
array(['b0002'], dtype=object)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With