Merge Multiple pandas DataFrames in Python (2 Examples) | Combine Horizontally | Inner & Outer Join

preview_player
Показать описание
Python code of this video:

import pandas as pd # Load pandas library

data1 = pd.DataFrame({"ID":range(10, 16), # Create first pandas DataFrame
"x1":range(100, 106),
"x2":["a", "b", "c", "d", "e", "f"],
"x3":range(27, 21, - 1)})
print(data1) # Print first pandas DataFrame

data2 = pd.DataFrame({"ID":range(14, 19), # Create second pandas DataFrame
"y1":["x", "y", "x", "x", "y"],
"y2":range(20, 25),
"y3":range(10, 1, - 2)})
print(data2) # Print second pandas DataFrame

data3 = pd.DataFrame({"ID":range(12, 20), # Create third pandas DataFrame
"z1":range(111, 119),
"z2":range(10, 2, - 1)})
print(data3) # Print third pandas DataFrame

from functools import reduce # Import reduce function

data_merge1 = reduce(lambda left, right: # Merge three pandas DataFrames
on = ["ID"]),
[data1, data2, data3])
print(data_merge1) # Print merged DataFrame

data_merge2 = reduce(lambda left, right: # Merge three pandas DataFrames
on = ["ID"],
how = "outer"),
[data1, data2, data3])
print(data_merge2) # Print merged DataFrame

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Thanks for the tutorial. But what if 2 or more of the merged dataframes have columns that are common to them?

OnLyhereAlone
Автор

"FutureWarning: Passing 'suffixes' which cause duplicate columns" Gettying this warning. How to merge data frames with duplicate values

saurabhamrutkar
Автор

What IDE do you use to run the scripts?

andrespjp