It is like this if you write the conclusion first.
df3 = df1[~df1['row1'].isin(df2['row2'])]
From a data frame df1 The column'row1' of the data frame and Compare column'row2' of the data frame df2 to be compared, I want to extract a row from df1 that has row1 that does not exist in row2.
It is like this when written in SQL.
SELECT * FROM df1
WHER df1.row1 NOT IN (SELECT row2 FROM df2)
Data of df1
color | row1 |
---|---|
red | eagle |
blue | shark |
yellow | lion |
green | elephant |
white | tiger |
black | world |
df2 data
name | row2 |
---|---|
Sela | shark |
Leo | lion |
Tusk | elephant |
Amu | tiger |
Data you want to retrieve from df1
color | row1 |
---|---|
red | eagle |
black | world |
Zyuohger.py
import pandas as pd
df1 = pd.DataFrame({
'color':['red', 'blue', 'yellow','green', 'white','black',],
'row1' :['eagle','shark','lion', 'elephant','tiger','world',],
})
df2 = pd.DataFrame({
'name':['Sela', 'Leo', 'Tusk', 'Amu',],
'row2':['shark','lion','elephant','tiger',],
})
df3 = df1[~df1['row1'].isin(df2['row2'])]
print df3
When executed, it will be as follows.
$ python Zyuohger.py
color row1
0 red eagle
5 black world
Recommended Posts