Home > Article > Backend Development > How do you merge DataFrames by index in Python using Pandas?
Merging Dataframes by Index
Merging dataframes by index is a straightforward task that enables us to combine datasets based on their corresponding indices. This approach is advantageous when the datasets share a common set of row labels.
To merge dataframes by index, we have several options:
1. Merge Function
The pd.merge function offers an inner join by default, allowing us to merge on the indices:
<code class="python">import pandas as pd df1 = pd.DataFrame({ 'id': [278, 421], 'begin': [56, 18], 'conditional': [False, False], 'confidence': [0.0, 0.0], 'discoveryTechnique': [1, 1] }) df2 = pd.DataFrame({ 'concept': ['A', 'B'] }) result = pd.merge(df1, df2, left_index=True, right_index=True) print(result)</code>
Output:
id begin conditional confidence discoveryTechnique concept 0 278 56 False 0.0 1 A 1 421 18 False 0.0 1 B
2. Join Function
The df.join method provides a default left join:
<code class="python">result = df1.join(df2) print(result)</code>
Output:
id begin conditional confidence discoveryTechnique concept 0 278 56 False 0.0 1 A 1 421 18 False 0.0 1 B 2 665 48 False 0.0 0 NaN 3 1007 19 False 0.0 2 NaN 4 1636 32 False 0.0 0 NaN
3. Concat Function
The pd.concat function, with the axis=1 parameter, offers an outer join by default:
<code class="python">result = pd.concat([df1, df2], axis=1) print(result)</code>
Output:
id begin conditional confidence discoveryTechnique concept 0 278 56 False 0.0 1 A 1 421 18 False 0.0 1 B 2 665 48 False 0.0 0 NaN 3 1007 19 False 0.0 2 NaN 4 1636 32 False 0.0 0 NaN 5 NaN NaN NaN NaN NaN C
It's important to note that merging on the index is not considered bad practice and is useful when the index values are the primary identifiers. Shifting the index into a new column can be achieved using the reset_index method:
<code class="python">df2 = df2.reset_index() print(df2)</code>
Output:
index concept 0 0 A 1 1 B
The above is the detailed content of How do you merge DataFrames by index in Python using Pandas?. For more information, please follow other related articles on the PHP Chinese website!