How to Concatenate Data Vertically in Python?

Estimated read time 1 min read

To concatenate data vertically in Python, you can use the pd.concat() function from the pandas library with the axis=0 parameter. Here’s an example:

import pandas as pd

# Create sample data
data1 = {'Name': ['John', 'Emma', 'Mike'],
         'Age': [25, 30, 35]}
data2 = {'Name': ['Sarah', 'David'],
         'Age': [28, 32]}

# Create DataFrames
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Concatenate vertically
concatenated_df = pd.concat([df1, df2], axis=0)


In this example, we have two sample DataFrames, df1 and df2, each containing columns “Name” and “Age”. To concatenate them vertically, we use pd.concat() with axis=0. This combines the DataFrames row-wise, resulting in a new DataFrame called concatenated_df.

The output will be:

    Name  Age
0   John   25
1   Emma   30
2   Mike   35
0  Sarah   28
1  David   32

As you can see, the resulting DataFrame concatenated_df contains the combined rows of both df1 and df2.

By providing a list of DataFrames to pd.concat() and specifying axis=0, you can concatenate multiple DataFrames vertically. You can adjust the column names, order, and other aspects as needed for your specific use case.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply