~ 6 min read

Concatenating Pandas DataFrames – A How-To Guide

By: Adam Richardson
Share:

Introduction to Concatenating DataFrames

DataFrames are fundamentally important for data manipulation and analysis in Python. Concatenation is one of the core ways to combine two or more DataFrames into a single DataFrame.

Concatenation is the process of combining two or more DataFrames along a particular axis. In Pandas, the concat() function is performed along rows, axis=0, or columns, axis=1. The rows or columns are combined into a single DataFrame with the new length or width.

Here is an example of how to concatenate two DataFrames vertically with the concat() function:

import pandas as pd

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']})

df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
                    'B': ['B4', 'B5', 'B6', 'B7'],
                    'C': ['C4', 'C5', 'C6', 'C7'],
                    'D': ['D4', 'D5', 'D6', 'D7']})

frames = [df1, df2]

result = pd.concat(frames)

print(result)

The output of this code is:

    A   B   C   D
0  A0  B0  C0  D0
1  A1  B1  C1  D1
2  A2  B2  C2  D2
3  A3  B3  C3  D3
0  A4  B4  C4  D4
1  A5  B5  C5  D5
2  A6  B6  C6  D6
3  A7  B7  C7  D7

In the above code snippet, two DataFrames df1 and df2 are concatenated together vertically along axis 0. Then, the resultant DataFrame result is printed. It contains the rows from both DataFrames.

Concatenating DataFrames horizontally is performed similarly, by setting axis=1 in the concat() function.

In summary, concatenating Pandas DataFrames forms the basis for combining and manipulating data. The concat() function can be used to combine two or more DataFrames along row and/or column, forming a new DataFrame.

Understanding Different Ways of Concatenation

In addition to performing concatenation along the axis=0 and axis=1, there are other ways to concatenate. By understanding the different ways of concatenation, you can choose the most effective one for your data.

Append

Appending is a special case of concatenation along axis=0. This is equivalent to concatenation with axis=0. The append() method is an alternative to using the concat() function.

Here is an example of how to append two DataFrames with the append() method:

import pandas as pd

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']})

df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
                    'B': ['B4', 'B5', 'B6', 'B7'],
                    'C': ['C4', 'C5', 'C6', 'C7'],
                    'D': ['D4', 'D5', 'D6', 'D7']})

result = df1.append(df2)

print(result)

Join

Joining is an operation that combines rows based on column values. This is helpful when joining tables with a common field. This is equivalent to SQL Join. The merge() function is also used in conjunction with pandas DataFrames, but is more powerful than the join() method.

import pandas as pd

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']})

result = pd.merge(left, right, on='key')

print(result)

Combine

Combining allows two DataFrames to be joined based on common fields. Where the fields are missing or have unique values, the resulting DataFrame will include a null or unique identifier.

Here is an example of how to combine two DataFrames with the combine_first() method:

import pandas as pd
import numpy as np

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

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

result = df1.combine_first(df2)

print(result)

In summary, we have learned different ways of concatenating DataFrames in Pandas. The append() method adds rows to a DataFrame, joining based on columns. The join() method adds rows to a DataFrame, joining based on a common field. The merge() function also adds rows to a DataFrame, joining based on a common field, and includes advanced options for joining. The combine_first() method fills missing/nan values in the base DataFrame with values from the passed DataFrame.

Implementing Concatenation in Pandas

Concatenation in Pandas is implemented using the concat() method, which is flexible and powerful. It can combine more than two DataFrames, with option to handle variables like indexes, join types, and multilevel indexing.

Handling Indexes

Indexes are assigned to each row or column of a given DataFrame. When concatenating DataFrames, you can choose how to handle indexes. By default, concatenation along axis 0 preserves the indexes.

Here is an example of how to handle indexes with the ignore_index parameter for concatenating two DataFrames:

import pandas as pd

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

df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

result = pd.concat([df1, df2], ignore_index=True)

print(result)

Joining on Columns

Concatenation can be performed based on columns instead of indexes. This is done by specifying the join parameter.

Here is an example of how to join two DataFrames along columns with the join parameter set to β€˜outer’:

import pandas as pd

df3 = pd.DataFrame({'AA': ['AA0', 'AA1', 'AA2', 'AA3'],
                    'BB': ['BB0', 'BB1', 'BB2', 'BB3'],
                    'CC': ['CC0', 'CC1', 'CC2', 'CC3'],
                    'DD': ['DD0', 'DD1', 'DD2', 'DD3']})

df4 = pd.DataFrame({'AA': ['AA4', 'AA5', 'AA6', 'AA7'],
                    'BB': ['BB4', 'BB5', 'BB6', 'BB7'],
                    'CC': ['CC4', 'CC5', 'CC6', 'CC7'],
                    'DD': ['DD4', 'DD5', 'DD6', 'DD7']})

result = pd.concat([df3, df4], axis=1, join='outer')

print(result)

Multilevel Indexing

Concatenation along the rows can result in a DataFrame with multiple levels of indexing. This helps in organizing data hierarchically.

Here is an example of how to add multilevel indexes for concatenating two DataFrames:

import pandas as pd

df5 = pd.DataFrame({'AAA': [1, 2, 3],
                    'BBB': [4, 5, 6],
                    'CCC': [7, 8, 9]})

df6 = pd.DataFrame({'AAA': [1, 3, 4],
                    'DDD': [2, 2, 2],
                    'EEE': [5, 5, 5]})

result = pd.concat([df5, df6], keys=['DF5', 'DF6'])

print(result)

In summary, implementing concatenation in Pandas is a powerful way to manipulate data. You can control how indexes are handled, join multiple DataFrames, and employ multilevel indexing for organizing rows. The concat() function gives developers control for merging DataFrames.

Summary

In this article, we covered how to concatenate Pandas DataFrames in Python. We started with an introduction to concatenation and then dived into different ways of concatenation. We also explained how to implement concatenation in Pandas with code examples.

Concatenating DataFrames is a powerful technique in Python for data manipulation and analysis. It allows you to combine multiple datasets into a single DataFrame. By learning the concepts of concatenation, you can be able to effectively manipulate data using Pandas library.

Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts