One of the most common operations that one has to perform while working with pandas is merging of two or more DataFrames. This can be achieved using the merge() function.
We can perform the merge on DataFrames based on either the column or index. Let's take a look at each of them with examples.
Example 1: Pandas: Merge Two DataFrames by Column Names
import pandas as pd
dataframe_open = pd.DataFrame({'TimeSeries': ["2023-01", "2023-02", "2023-03"], 'Open': [14, 22, 45]})
dataframe_high = pd.DataFrame({'TimeSeries': ["2023-01", "2023-02", "2023-03"], 'High': [16, 22, 46]})
# Merge based on column name TimeSeries
merged_timeseries_df = pd.merge(dataframe_open, dataframe_high, on='TimeSeries')
print(merged_timeseries_df)
Output:
TimeSeries Open High
0 2023-01 14 16
1 2023-02 22 22
2 2023-03 45 46
Example 2: Pandas: Merge Multiple DataFrames by Column Names
import pandas as pd
dataframe_open = pd.DataFrame({'TimeSeries': ["2023-01", "2023-02", "2023-03"], 'Open': [14, 22, 45]})
dataframe_high = pd.DataFrame({'TimeSeries': ["2023-01", "2023-02", "2023-03"], 'High': [16, 22, 46]})
data_frame_low = pd.DataFrame({'TimeSeries': ["2023-01", "2023-02", "2023-03"], 'Low': [12, 20, 41]})
data_frame_close = pd.DataFrame({'TimeSeries': ["2023-01", "2023-02", "2023-03"], 'Close': [15, 21, 42]})
# Merge multiple df based on column name TimeSeries
merged_timeseries_df = pd.merge(dataframe_open, dataframe_high, on='TimeSeries')
merged_timeseries_df = pd.merge(merged_timeseries_df, data_frame_low, on='TimeSeries')
merged_timeseries_df = pd.merge(merged_timeseries_df, data_frame_close, on='TimeSeries')
print(merged_timeseries_df)
Output:
TimeSeries Open High Low Close
0 2023-01 14 16 12 15
1 2023-02 22 22 20 21
2 2023-03 45 46 41 42
Example 3: Pandas: Merge DataFrames based on Index
import pandas as pd
dataframe_open = pd.DataFrame({'Open': [14, 22, 45]}, index=["2023-01", "2023-02", "2023-03"])
dataframe_high = pd.DataFrame({'High': [16, 22, 46]}, index=["2023-01", "2023-02", "2023-03"])
dataframe_low = pd.DataFrame({'Low': [12, 20, 41]}, index=["2023-01", "2023-02", "2023-03"])
dataframe_close = pd.DataFrame({'Close': [15, 21, 42]}, index=["2023-01", "2023-02", "2023-03"])
# Merge the DataFrames based on index
merged_df = pd.merge(dataframe_open, dataframe_high, left_index=True, right_index=True)
merged_df = pd.merge(merged_df, dataframe_low, left_index=True, right_index=True)
merged_df = pd.merge(merged_df, dataframe_close, left_index=True, right_index=True)
print(merged_df)
Output:
Open High Low Close
2023-01 14 16 12 15
2023-02 22 22 20 21
2023-03 45 46 41 42
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- How to know your iPhone Model Name - iOS
- Notepad++ : Cannot load 64 or 32bit plugin Error. - NotepadPlusPlus
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- Why I see Download pre-built shared indexes in IntelliJ - HowTos
- Android: programmatically turn Bluetooth on or off using Java code - Android
- How to show or hide columns in SharePoint Online List Library from - SharePoint
- Git Revision Questions Before the Interview - Git
- The Motivation Behind Generics in Java Programming - Java