Pandas How To Uncategorized How to join two dataframes on rows

How to join two dataframes on rows

You can join two pandas DataFrames along the rows (i.e., concatenate them vertically) using the concat method in pandas.

Here’s an example of how to concatenate two DataFrames vertically:

import pandas as pd

df1 = pd.DataFrame({'key': [1, 2, 3, 4, 5], 'col1': [10, 20, 30, 40, 50]})
df2 = pd.DataFrame({'key': [2, 4, 6, 8, 10], 'col2': [100, 200, 300, 400, 500]})

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

The concat method takes a list of DataFrames as input and concatenates them vertically. The ignore_index parameter is used to reset the index of the resulting DataFrame, which can be useful if the indices of the input DataFrames are not meaningful for the concatenated result.

In this example, the result will be a new DataFrame that has the rows from both input DataFrames stacked vertically. The resulting DataFrame will have two columns: key and col1, col2.


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post