How do you merge DataFrames by index in Python using Pandas?

Susan Sarandon
Release: 2024-11-01 17:35:02
Original
175 people have browsed it

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>
Copy after login

Output:

   id  begin conditional confidence discoveryTechnique concept
0 278    56       False        0.0                  1       A
1 421    18       False        0.0                  1       B
Copy after login

2. Join Function

The df.join method provides a default left join:

<code class="python">result = df1.join(df2)

print(result)</code>
Copy after login

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
Copy after login

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>
Copy after login

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
Copy after login

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>
Copy after login

Output:

   index concept
0      0       A
1      1       B
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!