Add Rows from Pandas DataFrame to Another
Welcome to this tutorial on how to add a row from one Pandas DataFrame to another.
In this tutorial, you’ll learn the different methods for adding rows from one DataFrame to another.
Using concat()
The concat()
function in Pandas is a powerful tool for combining DataFrames. First, let’s create two sample DataFrames.
import pandas as pd df1 = pd.DataFrame({ 'Plan_ID': [1, 2, 3], 'Monthly_Cost': [30, 40, 50], 'Data_Limit': [2, 4, 6] }) df2 = pd.DataFrame({ 'Plan_ID': [4], 'Monthly_Cost': [60], 'Data_Limit': [8] }) print("DataFrame 1:") print(df1) print("\nDataFrame 2:") print(df2)
Output:
DataFrame 1: Plan_ID Monthly_Cost Data_Limit 0 1 30 2 1 2 40 4 2 3 50 6 DataFrame 2: Plan_ID Monthly_Cost Data_Limit 0 4 60 8
Now, let’s use the concat()
function to combine these DataFrames.
result_df = pd.concat([df1, df2], ignore_index=True) print("Resulting DataFrame:") print(result_df)
Output:
Resulting DataFrame: Plan_ID Monthly_Cost Data_Limit 0 1 30 2 1 2 40 4 2 3 50 6 3 4 60 8
In the output, you can see that the row from df2
has been successfully added to df1
.
We used ignore_index=True
to reindex the resulting DataFrame.
Using loc and iloc
These properties allow for label-based and integer-based indexing, respectively, and can be useful for more granular control over row addition.
We continue with the same df1
and df2
DataFrames as in the previous examples.
Using loc
The loc
attribute allows you to add a row by specifying the label of the new index.
# Add row using loc df1.loc[len(df1)] = df2.loc[0] print("Resulting DataFrame:") print(df1)
Output:
Resulting DataFrame: Plan_ID Monthly_Cost Data_Limit 0 1 30 2 1 2 40 4 2 3 50 6 3 4 60 8
Here, we added the row from df2
to df1
using the loc
attribute. We specified the new index as len(df1)
, which appends the row at the end.
Using iloc
Similarly, you can use iloc
for integer-location based indexing to add rows.
# Reset df1 for demonstration df1 = df1.iloc[:-1] # Add row using iloc df1.loc[len(df1)] = df2.iloc[0] print("Resulting DataFrame:") print(df1)
Output:
Resulting DataFrame: Plan_ID Monthly_Cost Data_Limit 0 1 30 2 1 2 40 4 2 3 50 6 3 4 60 8
Conditional Addition of Rows Based on Column Values
This conditional addition can be achieved using boolean indexing.
First, let’s create two sample DataFrames:
df6 = pd.DataFrame({ 'Plan_ID': [1, 2, 3], 'Monthly_Cost': [30, 40, 50], 'Data_Limit': [2, 4, 6] }) df7 = pd.DataFrame({ 'Plan_ID': [2, 3, 4], 'Monthly_Cost': [35, 55, 60], 'Data_Limit': [4, 7, 8] }) print("DataFrame 6:") print(df6) print("\nDataFrame 7:") print(df7)
Output:
DataFrame 6: Plan_ID Monthly_Cost Data_Limit 0 1 30 2 1 2 40 4 2 3 50 6 DataFrame 7: Plan_ID Monthly_Cost Data_Limit 0 2 35 4 1 3 55 7 2 4 60 8
Suppose you want to add only the rows from DataFrame df7
where the Monthly_Cost
is greater than 50 to df6
.
filtered_df7 = df7[df7['Monthly_Cost'] > 50] result_df_conditional = df6.append(filtered_df7, ignore_index=True, sort=False) print("Resulting DataFrame:") print(result_df_conditional)
Output:
Resulting DataFrame: Plan_ID Monthly_Cost Data_Limit 0 1 30 2 1 2 40 4 2 3 50 6 3 3 55 7 4 4 60 8
Adding Rows from Multiple DataFrames (Batch Addition)
First, let’s create three DataFrames to simulate a situation where you need to combine rows from multiple sources.
df8 = pd.DataFrame({ 'Plan_ID': [1, 2], 'Monthly_Cost': [20, 25], 'Data_Limit': [1, 2] }) df9 = pd.DataFrame({ 'Plan_ID': [3, 4], 'Monthly_Cost': [30, 35], 'Data_Limit': [3, 4] }) df10 = pd.DataFrame({ 'Plan_ID': [5, 6], 'Monthly_Cost': [40, 45], 'Data_Limit': [5, 6] }) print("DataFrame 8:") print(df8) print("\nDataFrame 9:") print(df9) print("\nDataFrame 10:") print(df10)
Output:
DataFrame 8: Plan_ID Monthly_Cost Data_Limit 0 1 20 1 1 2 25 2 DataFrame 9: Plan_ID Monthly_Cost Data_Limit 0 3 30 3 1 4 35 4 DataFrame 10: Plan_ID Monthly_Cost Data_Limit 0 5 40 5 1 6 45 6
One of the simplest ways to perform batch row addition is by using the concat()
function, where you can pass a list of DataFrames to concatenate.
result_df_batch = pd.concat([df8, df9, df10], ignore_index=True, sort=False) print("Resulting DataFrame:") print(result_df_batch)
Output:
Resulting DataFrame: Plan_ID Monthly_Cost Data_Limit 0 1 20 1 1 2 25 2 2 3 30 3 3 4 35 4 4 5 40 5 5 6 45 6
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.