Pandas How To Uncategorized How to append to empty dataframe in Pandas

How to append to empty dataframe in Pandas

To append data to an empty dataframe in Python, you can use the Pandas library. Here’s an example:

import pandas as pd

# Instantiate an empty DataFrame with pre-defined column names
df = pd.DataFrame(columns=['Column1', 'Column2', 'Column3'])

# Append data to the DataFrame
df = df.append({'Column1': 1, 'Column2': 2, 'Column3': 3}, ignore_index=True)

# Display the DataFrame
print(df)

In this revised example, we first instantiate an empty DataFrame with pre-defined column names using the pd.DataFrame() function. Then, we utilize the df.append() method to add a row of data to the DataFrame. The ignore_index=True parameter is used to signal to Pandas to automatically assign a new index to the appended row.

Finally, we display the resulting DataFrame to verify that the values were appended correctly.

Tags:

Leave a Reply

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

Related Post