[Python] Join two tables with pandas

What do you want to do

I want to combine the following two tables into one based on the ID. 図1.png

How to do

pd.merge(Table_A,Table_B,how="XXXX",on="ID")

You can select the joining method with how = "XXXX".

how="inner" (inner join) Extract and join the IDs that are common to the two tables. 図2.png

pd.merge(Table_A,Table_B,how="inner",on="ID")

result 図3.png

how="outer" (outer join) Extract and join all the IDs of the two tables.

図4.png

pd.merge(Table_A,Table_B,how="outer",on="ID")

result 図5.png

Where there is no data, it is NaN.

how="left" (left join) Join only the IDs of the table on the left. 図6.png

pd.merge(Table_A,Table_B,how="left",on="ID")

result 図7.png

how="right" (right join) Join only the IDs of the table on the right. 図8.png

pd.merge(Table_A,Table_B,how="right",on="ID")

図9.png

end

Since there were few articles written in Venn diagrams, I pushed it out as much as possible.

Recommended Posts

[Python] Join two tables with pandas
Read csv with python pandas
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Excel aggregation with Python pandas Part 1
[Python] Format when to_csv with pandas
[Python beginner] Join two lists alternately (5 lines).
Excel aggregation with Python pandas Part 2 Variadic
[Easy Python] Reading Excel files with pandas
Join an online judge with Python 3.x
Load AWS-RDS / PostgreSQL tables with AWS-Lambda + Python
FizzBuzz with Python3
Scraping with Python
My pandas (python)
Statistics with python
Scraping with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Join data with main key (required) and subkey (optional) in Python pandas
python pandas notes
Excel with Python
Microcomputer with Python
Cast with python
Extract text from PowerPoint with Python! (Compatible with tables)
Automatic operation of Chrome with Python + Selenium + pandas
Generate two correlated pseudo-random numbers (with Python sample)
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Update multiple tables at once with pandas to_sql
Two things I was happy about with Python 3.9
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Quickly visualize with Pandas
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Processing datasets with pandas (1)
Run prepDE.py with python3
Bootstrap sampling with Pandas
Convert 202003 to 2020-03 with pandas
Processing datasets with pandas (2)
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python