Pandas How To Uncategorized How to append two dataframes

How to append two dataframes

You can append two DataFrames in Pandas using the concat method:

import pandas as pd

# Create two dataframes
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [4, 5, 6], 'B': [7, 8, 9]})

# Append df2 to df1
df_concatenated = pd.concat([df1, df2])

# Print the concatenated dataframe
print(df_concatenated)

 

This will create two dataframes, df1 and df2, each with two columns, ‘A’ and ‘B’, and three rows. The concat function is used to concatenate the two dataframes along the rows, resulting in a new dataframe with six rows.

Note that concat returns a new dataframe, so you should assign the result to a variable if you want to keep the concatenated dataframe. Additionally, the concat function can take a list of dataframes to concatenate, so you can append more than two dataframes by including them in the list.

Tags:

Leave a Reply

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

Related Post