
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to combine dataframes in Pandas?
To combine dataframes in Pandas, we will show some examples. We can easily combine DataFrames or even Series in Pandas. Pandas is an open-source Python Library providing high-performance data manipulation and analysis tool using its powerful data structures. A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns.
Combine DataFrames using Inner Join
Example
Let us combine the dataframes using inner join in Python
import pandas as pd # Create Dictionaries dct1 = {'Player':['Jacob','Steve','David','John','Kane'], 'Age':[29, 25, 31, 26, 27]} dct2 = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]} # Create DataFrame from Dictionary elements using pandas.dataframe() df1 = pd.DataFrame(dct1) df2 = pd.DataFrame(dct2) print("DataFrame1 = \n",df1) print("\nDataFrame2 = \n",df2) # Combining DataFrames using inner join res = pd.concat([df1, df2], axis=1, join='inner') print("\nCombined DataFrames = \n",res)
Output
DataFrame1 = Player Age 0 Jacob 29 1 Steve 25 2 David 31 3 John 26 4 Kane 27 DataFrame2 = Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Combined DataFrames = Player Age Rank Points 0 Jacob 29 1 100 1 Steve 25 2 87 2 David 31 3 80 3 John 26 4 70 4 Kane 27 5 50
Combine DataFrames using append()
Example
In this example, we will us combine the dataframes using append() in Python
import pandas as pd # Create Dictionaries dct1 = {'Player':['Steve','David'], 'Age':[29, 25,]} dct2 = {'Player':['John','Kane'], 'Age':[31, 27]} # Create DataFrame from Dictionary elements using pandas.dataframe() df1 = pd.DataFrame(dct1) df2 = pd.DataFrame(dct2) print("DataFrame1 = \n",df1) print("\nDataFrame2 = \n",df2) # Combining DataFrames using append() res = df1.append(df2) print("\nCombined DataFrames = \n",res)
Output
DataFrame1 = Player Age 0 Steve 29 1 David 25 DataFrame2 = Player Age 0 John 31 1 Kane 27 Combined DataFrames = Player Age 0 Steve 29 1 David 25 0 John 31 1 Kane 27
Combine DataFrames using concat()
Example
In this example, we will us combine the dataframes using concat() in Python −
import pandas as pd # Create Dictionaries dct1 = {'Player':['Steve','David'], 'Age':[29, 25,]} dct2 = {'Player':['John','Kane'], 'Age':[31, 27]} # Create DataFrame from Dictionary elements using pandas.dataframe() df1 = pd.DataFrame(dct1) df2 = pd.DataFrame(dct2) print("DataFrame1 = \n",df1) print("\nDataFrame2 = \n",df2) # Combining DataFrames using concat() res = pd.concat([df1, df2]) print("\nCombined DataFrames = \n",res)
Output
DataFrame1 = Player Age 0 Steve 29 1 David 25DataFrame2 = Player Age 0 John 31 1 Kane 27 Combined DataFrames = Player Age 0 Steve 29 1 David 25 0 John 31 1 Kane 27
- Related Articles
- How to append two DataFrames in Pandas?
- How are dataframes in Pandas merged?
- How to plot histograms from dataframes in Pandas using Matplotlib?
- Python - How to Concatenate more than two Pandas DataFrames?
- How to compare two DataFrames in Python Pandas with missing values
- Python - How to Concatenate Two or More Pandas DataFrames along rows?
- Plotting multiple dataframes using Pandas functionality
- Python - Concatenate Pandas DataFrames Without Duplicates
- Plotting Pandas DataFrames in Pie Charts using Matplotlib
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- How does pandas series combine() method work?
- Merge, Join and Concatenate DataFrames using Pandas
- Python Pandas – Find the Difference between two Dataframes
- Plot 95% confidence interval errorbar Python Pandas dataframes in Matplotlib
- How to combine a pandas Series with Scalar using Series.combine() method?

Advertisements