[Python] Extracts data frames that do not match a specific column with other data frames of Pandas

Write this

It is like this if you write the conclusion first.

df3 = df1[~df1['row1'].isin(df2['row2'])]

I want to do something like this

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

Implementation

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

result

When executed, it will be as follows.

$ python Zyuohger.py
   color   row1
0    red  eagle
5  black  world

Recommended Posts

[Python] Extracts data frames that do not match a specific column with other data frames of Pandas
pandas Fetch the name of a column that contains a specific character
Do not change the order of columns when concatenating pandas data frames.
Tips: [Python] Extract only lines that do not contain a specific string
A server that echoes data POSTed with flask / python
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
Detect objects of a specific color and size with Python
A collection of methods used when aggregating data with pandas
A set of script files that do wordcloud in Python3
The story of creating a bot that displays active members in a specific channel of slack with python
A Python one-liner that pretty prints json. Do not escape Japanese.
[Python] How to force a method of a subclass to do something specific
The story of making a module that skips mail with python
Introduction of "scikit-mobility", a library that allows you to easily analyze human flow data with Python (Part 1)
A summary of Python e-books that are useful for free-to-read data analysis
Code reading of faker, a library that generates test data in Python
Add a Python data source with Redash
Recommendation of Altair! Data visualization with Python
Let's do MySQL data manipulation with Python
A typed world that begins with Python
Example of efficient data processing with PANDAS
Python practice data analysis Summary of learning that I hit about 10 with 100 knocks
Around the authentication of PyDrive2, a package that operates Google Drive with Python
Separately install a version of Python that is not pre-installed on your Mac
Extract periods that match a particular pattern from pandas time series qualitative data
A memorandum of method often used when analyzing data with pandas (for beginners)
[Introduction to Python] How to get the index of data with a for statement
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Create test data like that with Python (Part 1)
Automatic operation of Chrome with Python + Selenium + pandas
Create a page that loads infinitely with python
Solve A ~ D of yuki coder 247 with python
A story stuck with handling Python binary data
Folium: Visualize data on a map with Python
Make holiday data into a data frame with pandas
Extract data from a web page with Python
[Python Data Frame] When the value is empty, fill it with the value of another column.
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
Predicting the goal time of a full marathon with machine learning-③: Visualizing data with Python-
Get the value of a specific key in a list from the dictionary type in the list with Python
Consolidate a large number of CSV files in folders with python (data without header)
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
When reading a csv file with read_csv of pandas, the first column becomes index
I tried scraping food recall information with Python to create a pandas data frame