How to Concatenate Rows of Dataframes in Python

To concatenate rows of a dataframe in Python, you can use the pd.concat() function by specifying axis=0.

The following example shows how to combine dataframe rows in Python.

Using concat() Function

We can use the concat() function to combine rows of dataframes.

Suppose we have the following dataframes:

# Import pandas library
import pandas as pd

# Create two dataframes
df1 = pd.DataFrame({'Product_Code': ['A-101', 'A-102', 'A-103'],
                    'Product_Name': ['Laptop', 'Mobile', 'Printer'],
                    'Price': [4500, 550, 250],
                    'Status': [1, 1, 1]})

df2 = pd.DataFrame({'Product_Code': ['B-101', 'B-102', 'B-104'],
                    'Product_Name': ['Keyboard', 'Scanner', 'Mouse'],
                    'Price': [500, 350, 50],
                    'Status': [1, 1, 1]})

# Combine rows
combined_rows = pd.concat([df1, df2], axis=0)

# Show combined rows
print(combined_rows)

Output: 👇️

  Product_Code Product_Name  Price  Status
0        A-101       Laptop   4500       1
1        A-102       Mobile    550       1
2        A-103      Printer    250       1
0        B-101     Keyboard    500       1
1        B-102      Scanner    350       1
2        B-104        Mouse     50       1

In this example, we use the pd.concat() function to combine the rows of df1 and df2 into a single dataframe combined_rows.

Conclusion

We can use the pd.concat() function by specifying axis=0 to concatenate rows of dataframes in Python. This method provides a convenient way to merge rows from multiple dataframes.