How to Stack Multiple Pandas DataFrames?


The vast universe of Python includes a shining constellation named Pandas. Recognized globally for its might in data management and manipulation, it empowers data analysts with tools that act as an extension of their thoughts, transforming ideas into reality.

The crux of this discussion lies in a particular feature of Pandas, the fusion of DataFrames along an axis. When the challenge is to blend information from diverse origins or conglomerate data for a comprehensive analysis, Pandas offers a basket of functions like concat(), append(), and merge(). The onus is on us to pick the tool that aligns with our needs, a choice that we'll make simpler in this detailed exposition.

In this captivating article, our focus shall be directed towards exploring the following methods of combining DataFrames:

  • a. The Marvels of List Comprehension and Pandas.concat

  • b. The Intricacies of Pandas.concat with axis=0 and keys

  • c. The Elegance of numpy.vstack

  • d. The Magic of Pandas.DataFrame.append

  • e. The Versatility of Pandas.concat

Syntax: A Glimpse into the Pandas Realm

Throughout this awe-inspiring journey, we shall traverse the realm of the pandas library, which stands as a gateway to the functions enabling DataFrame fusion. Let us now embark on a brief overview of the syntax for each function that shall illuminate our path:

concat()

pd.concat(objs, axis=0, join='outer', keys=None, ignore_index=False)

pd.concat() concatenates dataframes or series along a specified axis, with options for join types and index handling.

append()

df1.append(df2, ignore_index=False, verify_integrity=False, sort=False)

df1.append(df2) appends the rows of df2 to df1, resetting the index by default.

vstack()

numpy.vstack(tup)

numpy.vstack(tup) stacks arrays vertically (row-wise), provided they have the same number of columns.

Examples

Using list comprehension and pandas.concat

Imagine a realm where you possess a list of ethereal DataFrames, longing to unite them in vertical harmony. Fear not, for the secrets of list comprehension and pandas.concat shall serve as your guiding light:

import pandas as pd

# List of DataFrames
dfs = [df1, df2, df3]

# Concatenate the DataFrames vertically
result = pd.concat(dfs)

print(result)

Output

    A   B
0  A0  B0
1  A1  B1
2  A2  B2
3  A3  B3
4  A4  B4
5  A5  B5

Using pandas.concat with axis=0 and keys

When seeking to weave a tapestry of DataFrames entwined with a hierarchical index, the path of Pandas.concat with axis=0 and keys reveals its true splendor:

import pandas as pd

# DataFrames df1 and df2 defined above

# Concatenate the DataFrames vertically with keys
result = pd.concat([df1, df2], keys=['df1', 'df2'])

print(result)

Output

       A   B
df1 0  A0  B0
    1  A1  B1
    2  A2  B2
df2 3  A3  B3
    4  A4  B4
    5  A5  B5

Using numpy.vstack

Behold, a realm where the very essence of numpy.vstack lies dormant, ready to awaken the power of vertical DataFrame concatenation:

import pandas as pd
import numpy as np

# DataFrames df1 and df2 defined above

# Convert DataFrames to numpy arrays and stack them vertically
result_array = np.vstack([df1.values, df2.values])

# Convert the stacked numpy array back to a DataFrame
result = pd.DataFrame(result_array, columns=df1.columns)

print(result)

Output

    A   B
0  A0  B0
1  A1  B1
2  A2  B2
3  A3  B3
4  A4  B4
5  A5  B5

Using pandas.DataFrame.append

Behold, a mystical journey where the powers of Pandas.DataFrame.append enable the merging of DataFrames along the axis of dreams, harmonizing their essence:

import pandas as pd

# DataFrames df1 and df2 defined above

# Append df2 to df1
result = df1.append(df2)

print(result)

Output

    A   B
0  A0  B0
1  A1  B1
2  A2  B2
3  A3  B3
4  A4  B4
5  A5  B5

Using pandas.concat

Witness the versatility of the Pandas.concat() function, as it weaves its magic, seamlessly uniting DataFrames along a chosen axis:

import pandas as pd

# DataFrames df1 and df2 defined above

# Concatenate the DataFrames vertically (default axis=0)
result = pd.concat([df1, df2])

print(result)

Output

    A   B
0  A0  B0
1  A1  B1
2  A2  B2
3  A3  B3
4  A4  B4
5  A5  B5

Conclusion

Pandas, with its abundant functions to amalgamate or merge multiple DataFrames, empowers you to handle an array of data manipulation tasks. This understanding is akin to holding the key to a vast treasure trove, promising riches of insights.

Though, remember, we've merely skimmed the surface. There lies an ocean of possibilities with other functions such as join() and update(), each holding its unique powers. The journey to master Pandas is an expedition into the heart of data analysis, a journey promising the thrill of discovery and the joy of understanding.

Updated on: 28-Aug-2023

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements