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!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Simple CSS Grid Example - CSS
- Android Studio : Implement abstract methods shortcut - Android-Studio
- Add Custom header and footer to Windows Notepad file - NotepadPlusPlus
- [fix] Docker: OCI runtime exec failed unable to start container process - Docker
- Turn Off Google Analytics intelligence Alert Emails - Google
- How to Search Something (string) in Android Studio Project like Eclipse - Android-Studio
- Word count in Sublime Text Editor - Sublime-Text
- PowerShell: If Else ElseIf Statements Examples - Powershell