Syntax error How to combine dataframes in Pandas?

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
Updated on: 2022-09-15T12:52:38+05:30

816 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements