Python | Pandas Merging, Joining, and Concatenating

Pandas provide various facilities for easily combining together Series, DataFrame, and Panel objects with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations.

Merge, Join and Concatenate DataFrames using Panda

Example #1 : DataFrames Concatenation

concat() function does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.

# Python program to concatenate
# dataframes using Panda

# Creating first dataframe
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']},
                    index = [0, 1, 2, 3])

# Creating second dataframe
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
                    'B': ['B4', 'B5', 'B6', 'B7'],
                    'C': ['C4', 'C5', 'C6', 'C7'],
                    'D': ['D4', 'D5', 'D6', 'D7']},
                    index = [4, 5, 6, 7])

# Creating third dataframe
df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'],
                    'B': ['B8', 'B9', 'B10', 'B11'],
                    'C': ['C8', 'C9', 'C10', 'C11'],
                    'D': ['D8', 'D9', 'D10', 'D11']},
                    index = [8, 9, 10, 11])

# Concatenating the dataframes
pd.concat([df1, df2, df3])

Output:

Concatenation

 

Example #2 : DataFrames Merge

Pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame objects.

# Python program to merge
# dataframes using Panda

# Dataframe created
left = pd.DataFrame({'Key': ['K0', 'K1', 'K2', 'K3'],
                    'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3']})

right = pd.DataFrame({'Key': ['K0', 'K1', 'K2', 'K3'],
                      'C': ['C0', 'C1', 'C2', 'C3'],
                      'D': ['D0', 'D1', 'D2', 'D3']})
                      
# Merging the dataframes                      
pd.merge(left, right, how ='inner', on ='Key')

Output:

Merging

 
Code #3 : DataFrames Join

# Python program to join
# dataframes using Panda

left = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3']},
                    index = ['K0', 'K1', 'K2', 'K3'])

right = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],
                      'D': ['D0', 'D1', 'D2', 'D3']},
                      index = ['K0', 'K1', 'K2', 'K3'])
                      
# Joining the dataframes                      
left.join(right)

Output:

Joining

 
Useful Methods:

Function Description
concat() Method is used to concatenate two or more DataFrames together
concat(keys) Method is used to identify what DataFrame the rows came from
append() Method is called on a DataFrame to concatenate another DataFrame to the end
merge() Method merges the values in two DataFrames based on common values in multiple columns
join() Method concatenates two DataFrames vertically when they share the same index

 
More on Pandas

  1. Python | Pandas Series
  2. Python | Pandas DataFrame
  3. Python | Pandas Working With Text Data
  4. Python | Pandas Working with Dates and Times


My Personal Notes arrow_drop_up