Append DataFrames to each other
Introduction
To append a DataFrame to a blank DataFrame in Pandas, you can use the concat()
function. The concat()
function concatenates two or more DataFrames along a particular axis.
Here is an example of how to create a blank DataFrame and append another DataFrame to it:
import pandas as pd
# create a blank DataFrame
blank_df = pd.DataFrame()
# create a DataFrame to append
data = {'col1': [1, 2], 'col2': [3, 4]}
df_to_append = pd.DataFrame(data)
# append the DataFrame to the blank DataFrame
result = pd.concat([blank_df, df_to_append])
print(result)
In the above example, we first create a blank DataFrame called blank_df
. Then, we create a DataFrame called df_to_append
with some data. Finally, we use the concat()
function to append df_to_append
to blank_df
, and store the result in a variable called result
.
The output of the above code will be:
col1 col2
0 1 3
1 2 4
This shows that the df_to_append
DataFrame has been successfully appended to the blank_df
DataFrame.