0% found this document useful (0 votes)
14 views4 pages

UnitIV 1

Uploaded by

pradeeplavrp09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

UnitIV 1

Uploaded by

pradeeplavrp09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Discuss the Combining Datasets operations (Concat, Append, Merge and Join) in Pandas with

suitable examples
In Pandas, combining datasets is a common operation to manipulate and analyze data
effectively. Operations like Concat, Append, Merge, and Join allow us to work with multiple DataFrames.
Below is a detailed discussion with examples for each operation.

Combining datasets operations in pandas: concat, append, merge, and join. Each of these
functions has its use cases and methods of combining dataframes.

1. Concatenation

The pd.concat() function is used to concatenate DataFrames along a particular axis (row-wise or column-
wise).

Key Points:

Combines data by stacking.

Aligns on the index by default.

axis=0 for rows (default), axis=1 for columns.

Example:

import pandas as pd

# Sample DataFrames

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

# Concatenating along rows

result = pd.concat([df1, df2], axis=0)

print(result)

Output:

A B

0 1 3

1 2 4

0 5 7

1 6 8
2. Append

The DataFrame.append() method is used to append rows of one DataFrame to another.

Key Points:

Similar to concat() with axis=0.

Deprecated in favor of concat() in newer Pandas versions.

Example:

# Appending df2 to df1

result = df1.append(df2, ignore_index=True)

print(result)

Output:

A B

0 1 3

1 2 4

2 5 7

3 6 8

3. Merge

The pd.merge() function is used to combine DataFrames based on keys (like SQL joins).

Key Points:

Allows for one-to-one, many-to-one, and many-to-many joins.

on specifies the column(s) to merge on.

Supports inner, outer, left, and right joins.

Example:

# Sample DataFrames

df1 = pd.DataFrame({'ID': [1, 2], 'Name': ['Alice', 'Bob']})


df2 = pd.DataFrame({'ID': [2, 3], 'Score': [85, 90]})

# Merging on 'ID' column

result = pd.merge(df1, df2, on='ID', how='inner')

print(result)

Output:

ID Name Score

0 2 Bob 85

4. Join

The DataFrame.join() method is used to combine DataFrames on their index or on a key column.

Key Points:

Similar to merge, but defaults to index-based joins.

Can specify how parameter: left, right, outer, inner.

Example:

# Sample DataFrames

df1 = pd.DataFrame({'Name': ['Alice', 'Bob']}, index=[1, 2])

df2 = pd.DataFrame({'Score': [85, 90]}, index=[2, 3])

# Joining on index

result = df1.join(df2, how='inner')

print(result)

Output:

markdown

Name Score

2 Bob 85

You might also like