Formulir Kontak

Nama

Email *

Pesan *

Cari Blog Ini

Gambar

Pandas Merge A Comprehensive Guide

Pandas Merge - A Comprehensive Guide

Introduction

Pandas merge is a powerful function that allows you to join two DataFrames based on specific criteria. This operation is essential for data manipulation and analysis tasks, as it enables you to combine data from different sources into a single, unified DataFrame.

Types of Merges

Pandas merge supports various types of merges, each with its own unique characteristics:
  • left: Join on the keys from the left DataFrame.
  • right: Join on the keys from the right DataFrame.
  • outer: Join on the union of keys from both DataFrames.
  • inner: Join on the intersection of keys from both DataFrames.
  • cross: Join every row from the left DataFrame with every row from the right DataFrame.

Parameters

The merge function takes several important parameters:
  • how: Specifies the type of merge to perform.
  • left_on: The key or column from the left DataFrame to join on.
  • right_on: The key or column from the right DataFrame to join on.
  • left_index: Boolean indicating whether to use the left DataFrame's index as the join key.
  • right_index: Boolean indicating whether to use the right DataFrame's index as the join key.

Example

Consider the following two DataFrames:
 import pandas as pd  df1 = pd.DataFrame({'id': [1, 2, 3], 'name': ['John', 'Mary', 'Bob']}) df2 = pd.DataFrame({'id': [1, 4, 5], 'age': [25, 30, 35]}) 
To merge these DataFrames on the 'id' column using an inner join, we can use the following code:
 merged_df = pd.merge(df1, df2, how='inner', on='id') 
This operation will produce the following DataFrame:
    id  name  age 0  1  John  25 1  1  John  NaN 
As you can see, the merged DataFrame contains the rows where the 'id' columns in both DataFrames match. The resulting DataFrame has columns from both the original DataFrames.



Pinterest


1

Komentar