How to Append Row From Another Dataframe to Dataframe in Python
To append a row from another dataframe to a dataframe in Python, you can use the concat() function from the pandas library.
Method: Use concat() Function
pd.concat([df1, df2], ignore_index=True)
The following example shows how to append a row from another dataframe to a dataframe in Python using the concat() function.
Using concat() Function
We can use the concat() function to append rows from one dataframe to another.
Suppose we have the following dataframes:
# Import pandas library
import pandas as pd
# Create dataframe
Office_Stuff = pd.DataFrame({
'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
'Price': [4500, 550, 250, 50, 350, 50],
'Status': [1, 1, 1, 0, 1, 1]
})
# Create a new dataframe
New_Office_Stuff = pd.DataFrame({
'Date': ['03-03-2023', '03-03-2023'],
'Product_Code': ['A-104', 'B-105'],
'Product_Name': ['Chair', 'Table'],
'Price': [100, 200],
'Status': [1, 0]
})
# Append the new rows to the original dataframe
Office_Stuff = pd.concat([Office_Stuff, New_Office_Stuff], ignore_index=True)
# Print the updated dataframe
print(Office_Stuff)
Output: 👇️
Date Product_Code Product_Name Price Status
0 01-03-2023 A-101 Laptop 4500 1
1 01-03-2023 A-102 Mobile 550 1
2 01-03-2023 A-103 Printer 250 1
3 01-03-2023 B-101 Keyboard 50 0
4 02-03-2023 B-102 Scanner 350 1
5 02-03-2023 B-104 Mouse 50 1
6 03-03-2023 A-104 Chair 100 1
7 03-03-2023 B-105 Table 200 0
In this example, we use the concat() function to append rows from the New_Office_Stuff dataframe to the Office_Stuff dataframe.