How to Create an Empty Dataframe and Append Data in Python
To create an empty dataframe and append data in Python, you can use the DataFrame() and concat() functions from the pandas library.
The following example shows how to create an empty dataframe and append data to it in Python.
Using DataFrame() & concat() Function
We can use the DataFrame() and concat() functions to create an empty dataframe and append data to it.
Let’s see how to use the DataFrame() and concat() functions in Python:
# Import pandas library
import pandas as pd
# Declare columns
columns = ["Date", "Product_Code", "Product_Name", "Price", "Status"]
# Create empty dataframe
df = pd.DataFrame(columns=columns)
# Declare data
data = {'Date': '01-03-2023', 'Product_Code': 'A-101', 'Product_Name': 'Laptop', 'Price': 4500, 'Status': 1}
# Create new dataframe with data
new_df = pd.DataFrame(data, index=[0])
# Append data to dataframe
df = pd.concat([df, new_df])
# Show dataframe
print(df)
Output: 👇️
Date Product_Code Product_Name Price Status
0 01-03-2023 A-101 Laptop 4500 1
In this example, we use the DataFrame() function to create an empty dataframe df with specified columns.
We then create a new dataframe new_df with data and use the concat() function to append this data to the empty dataframe.
The output shows the updated dataframe with the appended data.
Conclusion
We can use the DataFrame() and concat() functions from the pandas library to create an empty dataframe and append data in Python.